Vlookup基于文本

时间:2018-01-05 09:26:25

标签: excel vba excel-vba

我正在尝试构建一个vlookup宏,它根据F列中的货币抵消多个位置.Lookup值基于日期。我目前有一个公式,我只是拖动,但我试图通过vba。

=VLOOKUP(A2,'FX Rates'!A:K,11,0)

enter image description here

根据货币,这些是我需要抵消的地方数量。

enter image description here

我试图录制一个宏,但没有多大帮助。

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

2 个答案:

答案 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)

这是一种没有VBA的方式。

将您的数据设置为一个表,并具有一个名为currency的命名范围,该范围包含带标题的不同缩写。然后使用下面的公式自动填充你的表(我假设现在将在G列中)

=VLOOKUP([@Date],'FX Rates'!A:K,MATCH([@CYY],currency,0),FALSE)

Data with formula and named range (currency

上面的命名范围currencyJ4:J14

正在查看的范围是:

Lookup range