Vlookup跨表格类型不匹配(VBA)

时间:2016-05-02 16:35:33

标签: excel vba excel-vba vlookup type-mismatch

我正在尝试将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结果吗?

1 个答案:

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