我正在尝试将vlookup插入特定的单元格(J2:O2),但我一直遇到类型不匹配。我不能为我的生活找出原因。这是我目前的代码:
Dim Basel_Sheet As Worksheet
Set Basel_Sheet = Sheets("Basel 3 EAD")
Dim Parent_Lookup As Range
Set Parent_Lookup = Sheets("Parent Mapping").Range("B2:E20000")
With Basel_Sheet
.Cells(2, "J").Formula = "=VLOOKUP(A2," & Parent_Lookup & ",4, False)"
...
...
... (formulas for remaining columns)
End With
我应该使用Application.WorksheetFunction.Vlookup
并定义变量来存储vlookup结果吗?
答案 0 :(得分:1)
在.Address
之后添加Parent_Lookup
,如下所示:
已编辑为@ScottCraner
包含.Address(1, 1, xlA1, True)
Dim Basel_Sheet As Worksheet
Set Basel_Sheet = Sheets("Basel 3 EAD")
Dim Parent_Lookup As Range
Set Parent_Lookup = Sheets("Parent Mapping").Range("B2:E20000")
With Basel_Sheet
.Cells(2, "J").Formula = "=VLOOKUP(A2," & Parent_Lookup.Address(1, 1, xlA1, True) & ",4, False)"
End With
另一个选择是将Parent_Lookup
声明为String
而不是Range
:
Dim Basel_Sheet As Worksheet
Set Basel_Sheet = Sheets("Basel 3 EAD")
Dim Parent_Lookup As String
Parent_Lookup = "'Parent Mapping'!B2:E20000"
With Basel_Sheet
.Cells(2, "J").Formula = "=VLOOKUP(A2," & Parent_Lookup & ",4, False)"
End With