我正在尝试在VBA中使用hLookup引用同一工作簿中的另一个工作表。我明确地按工作表引用了所有单元格,但仍然遇到hLookup的错误。
Sub updateSettles()
Dim settles As Worksheet
Dim swaps As Worksheet
Dim flys As Worksheet
Dim lookupRange As Range
Set settles = ThisWorkbook.Worksheets("Settles")
Set swaps = ThisWorkbook.Worksheets("Swaps")
Set flys = ThisWorkbook.Worksheets("Flys")
tradeDate = flys.Range("y2").Value
'update settles
lastcol = settles.Range("b3").End(xlToRight).Column
lastcol2 = swaps.Range("b1").End(xlToRight).Column
Set lookupRange = swaps.Range(swaps.Cells(2, 2), swaps.Cells(13, lastcol2))
For i = 2 To lastcol
settles.Range(4, i).Value = Application.WorksheetFunction.HLookup(settles.Range(settles.Cells(2, i)), swaps.Range(swaps.Cells(2, 2), swaps.Cells(13, lastcol2)), 12, False)
Next i
End Sub
答案 0 :(得分:0)
HLookup(settles.Range(settles.Cells(2, i)), swaps.Range(swaps.Cells(2, 2), swaps.Cells(13, lastcol2)), 12, False)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Range
方法需要字符串地址或两个单元格作为参数。它不能只使用一个单元格作为参数。你的意思是:
HLookup(settles.Cells(2, i).Value, swaps.Range(swaps.Cells(2, 2), swaps.Cells(13, lastcol2)), 12, False)