簡體   English   中英

Vlookup跨圖紙類型不匹配(VBA)

[英]Vlookup Across Sheets Type Mismatch (VBA)

我正在嘗試將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結果嗎?

添加.AddressParent_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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM