下面的代碼適用於表的第一行,但FX_Rate和FX_Date偏移函數保持導致錯誤,並且Table_Date和Table_Rate偏移函數完全不工作(全部在每個ElseIf爲每種貨幣)。有人可以告訴我我做錯了什麼嗎?我有這樣的感覺,我只是在代碼的錯誤區域有一些偏移功能,但我可能會離開。偏移函數創建錯誤(VBA)
該代碼旨在獲取表格中的數據,將日期與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
ITI不明白你想要做什麼。一兩張圖片可能會有所幫助。根據你的一般描述,我覺得有一個更簡單的解決方案。 –
@DougGlancy https://imgur.com/xJlcimb宏是爲了從左側獲取數據,並使用右側的歷史匯率來轉換金額(此刻,我只是試圖獲得利率,沒有完成實際的數學轉換)。 – 1937827
「VLOOKUP」如何? –