我正在尝试构建一个vlookup宏,它根据F列中的货币抵消多个位置.Lookup值基于日期。我目前有一个公式,我只是拖动,但我试图通过vba。
=VLOOKUP(A2,'FX Rates'!A:K,11,0)
根据货币,这些是我需要抵消的地方数量。
我试图录制一个宏,但没有多大帮助。
Sub test()
Dim Currency_EUR As String
Dim Lookup_Range As Range
Dim Currency_Rate As Single
Currency_EUR = Sheets("Recon").Range("F2")
Set Lookup_Range = Sheets("FX Rates").Range("A:K")
Currency_Rate = Application.WorksheetFunction.VLookup(Currency_EUR, Lookup_Range, 2, False)
End Sub
答案 0 :(得分:0)
我知道你可以在没有循环的情况下做到这一点,但我确信以下内容可行:
Sub foo()
Dim ws As Worksheet: Set ws = Sheets("Sheet1")
'change the sheet above to the one you are using
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
'check the last row in Sheet1
For i = 2 To LastRow 'loop from row 2 to last
CurrencyLookUp = ws.Cells(i, 6)
Select Case CurrencyLookUp
Case Is = "USD"
OffsetPlace = 2
Case Is = "CZK"
OffsetPlace = 3
Case Is = "DKK"
OffsetPlace = 4
Case Is = "GBP"
OffsetPlace = 5
Case Is = "SEK"
OffsetPlace = 6
Case Is = "NOK"
OffsetPlace = 7
Case Is = "AUD"
OffsetPlace = 8
Case Is = "CAD"
OffsetPlace = 9
Case Is = "GBP/USD"
OffsetPlace = 10
Case Is = "EUR"
OffsetPlace = 11
End Select
ws.Cells(i, 5).FormulaR1C1 = "=VLOOKUP(RC[-4],'FX Rates'!C1:C11," & OffsetPlace & ",FALSE)"
'above enter the formulas
Next i
End Sub
答案 1 :(得分:0)