偏移功能创建错误(VBA)

时间:2016-04-23 17:16:33

标签: excel vba excel-vba

下面的代码适用于表的第一行,但FX_Rate和FX_Date偏移函数会导致错误,而Table_Date和Table_Rate偏移函数根本不起作用(每个货币的每个ElseIf都有)。有人可以告诉我我做错了什么吗?我觉得我在代码的错误区域只有一些偏移函数,但我可能会离开。

代码用于获取表格中的数据,将日期与FX信息匹配并在当天返回汇率,然后转到表格中的下一个条目,直到它到达空白单元格。

如果这是一个愚蠢的问题,或者之前被问过,我道歉 - 我找不到答案。

Sub Convert()

Dim Table_Date As Range
Set Table_Date = Range("B12")

Dim FX_Date As Range
Set FX_Date = Range("L11")

Dim Table_Rate As Range
Set Table_Rate = Range("E12")

Dim FX_Rate As Range
Set FX_Rate = Range("M11")

Dim Table_Currency As Range
Set Table_Currency = Range("D12")

Dim x As Integer
Dim y As Integer
x = 2
y = 2

Do While x > 1
    If Table_Date = "" Then
        x = -1
    Else
        If Table_Currency = "USD" Then
            Do While y > 1
                If Table_Date = FX_Date Then
                    Table_Rate = FX_Rate
                    y = -1
                    x = -1
                Else
                    Set FX_Date = FX_Date.Offset(1, 0)
                    Set FX_Rate = FX_Rate.Offset(1, 0)
                End If
            Loop
            Set Table_Date = Table_Date.Offset(1, 0)
            Set Table_Rate = Table_Rate.Offset(1, 0)
        ElseIf Table_Currency = "EUR" Then
            Set FX_Rate = FX_Rate.Offset(0, 2)
            Do While y > 1
                If Table_Date = FX_Date Then
                    Table_Rate = FX_Rate
                    y = -1
                    x = -1
                Else
                    Set FX_Date = FX_Date.Offset(1, 0)
                    Set FX_Rate = FX_Rate.Offset(1, 0)
                End If
            Loop
            Set Table_Date = Table_Date.Offset(1, 0)
            Set Table_Rate = Table_Rate.Offset(1, 0)
        ElseIf Table_Currency = "JPY" Then
            Set FX_Rate = FX_Rate.Offset(0, 1)
            Do While y > 1
                If Table_Date = FX_Date Then
                    Table_Rate = FX_Rate
                    y = -1
                    x = -1
                Else
                    Set FX_Date = FX_Date.Offset(1, 0)
                    Set FX_Rate = FX_Rate.Offset(1, 0)
                End If
            Loop
            Set Table_Date = Table_Date.Offset(1, 0)
            Set Table_Rate = Table_Rate.Offset(1, 0)
        ElseIf Table_Currency = "SGD" Then
            Set FX_Rate = FX_Rate.Offset(0, 4)
            Do While y > 1
                If Table_Date = FX_Date Then
                    Table_Rate = FX_Rate
                    y = -1
                    x = -1
                Else
                    Set FX_Date = FX_Date.Offset(1, 0)
                    Set FX_Rate = FX_Rate.Offset(1, 0)
                End If
            Loop
            Set Table_Date = Table_Date.Offset(1, 0)
            Set Table_Rate = Table_Rate.Offset(1, 0)
        ElseIf Table_Currency = "HKD" Then
            Set FX_Rate = FX_Rate.Offset(0, 6)
            Do While y > 1
                If Table_Date = FX_Date Then
                    Table_Rate = FX_Rate
                    y = -1
                    x = -1
                Else
                    Set FX_Date = FX_Date.Offset(1, 0)
                    Set FX_Rate = FX_Rate.Offset(1, 0)
                End If
        Loop
        Set Table_Date = Table_Date.Offset(1, 0)
        Set Table_Rate = Table_Rate.Offset(1, 0)
    ElseIf Table_Currency = "NZD" Then
        Set FX_Rate = FX_Rate.Offset(0, 5)
        Do While y > 1
            If Table_Date = FX_Date Then
                Table_Rate = FX_Rate
                y = -1
                x = -1
            Else
                Set FX_Date = FX_Date.Offset(1, 0)
                Set FX_Rate = FX_Rate.Offset(1, 0)
            End If
        Loop
        Set Table_Date = Table_Date.Offset(1, 0)
        Set Table_Rate = Table_Rate.Offset(1, 0)
    Else
        Table_Rate = "Not a valid currency"
        y = -1
        x = -1
    End If
End If
Loop

End Sub

1 个答案:

答案 0 :(得分:1)

假设您通过一个循环,其中第一个循环是HKD,那么FX_Rate已经偏移了6列。如果不重置基点,则不能将其用作后续偏移的基点。此外,Select Case statement会优于所有重复代码。

这是对原始部分的简短重写,其中包含许多重复代码。

    Select Case LCase(Table_Currency)
        Case "usd"
            Set FX_Rate = Cells(FX_Rate.Row, "M")    'no offset from base point
        Case "eur"
            Set FX_Rate = Cells(FX_Rate.Row, "O")    'offset(0, 2)
        Case "jpy"
            Set FX_Rate = Cells(FX_Rate.Row, "N")    'offset(0, 1)
        Case "sgd"
            Set FX_Rate = Cells(FX_Rate.Row, "Q")    'offset(0, 4)
        Case "hkd"
            Set FX_Rate = Cells(FX_Rate.Row, "S")    'offset(0, 6)
        Case "nzd"
            Set FX_Rate = Cells(FX_Rate.Row, "R")    'offset(0, 5)
        Case Else
            Set FX_Rate = Nothing
    End Select

    If Not FX_Rate Is Nothing Then
        Do While y > 1
            If Table_Date = FX_Date Then
                Table_Rate = FX_Rate
                y = -1
                x = -1
            Else
                Set FX_Date = FX_Date.Offset(1, 0)
                Set FX_Rate = FX_Rate.Offset(1, 0)
            End If
        Loop
        Set Table_Date = Table_Date.Offset(1, 0)
        Set Table_Rate = Table_Rate.Offset(1, 0)
    Else
        Table_Rate = "Not a valid currency"
        y = -1
        x = -1
    End If

更广泛地查看您的示例数据图像以及您要完成的任务,似乎您的整个过程可以写成这样的内容。

Sub currencyConversionRates()
    Dim rw As Long, x As Variant, y As Variant

    With Worksheets("Sheet3")
        For rw = 12 To .Cells(Rows.Count, "B").End(xlUp).Row
            x = Application.Match(.Cells(rw, "D").Value2, .Rows(5), 0)
            y = Application.Match(.Cells(rw, "B").Value2, .Columns(12), 0)
            If Not (IsError(x) Or IsError(y)) Then
                .Cells(rw, "E") = .Cells(y, x).Value2
            Else
                .Cells(rw, "E") = "Not a valid currency"
            End If
        Next rw
    End With
End Sub

但是对于所有意图和目的,通过将以下公式放入E12,

也可以减少
=IF(AND(LEN(D12), B12>=$L$11), VLOOKUP(B12,L:R, MATCH(D12, L$5:R$5, 0), FALSE), "Not a valid currency")

......并填写。

currency_lookup