[英]Excel VBA: How to create VLOOKUP with unique range and multiple worksheets?
我還是vba的新手,目前正在嘗試編寫代碼,在其中從源工作表和輸出工作表執行vlookup並將其放入我的輸出工作表。 我需要對整個A列(每個月的行數有所不同)執行vlookup並將其放置在G列中。每當我嘗試運行它時,它都會顯示“錯誤438:對象不支持此屬性或方法”。 我在網上某個地方找到了此代碼,並嘗試對其進行更改以匹配我的數據。 我猜我的sourceSheet沒有分配給工作表,即使我指定了它也可以嗎? 誰能幫忙:
這是我的代碼:
Sub Vlookup()
Dim SourceLastRow As Long
Dim OutputLastRow As Long
Dim sourceSheet As Worksheet
Dim outputSheet As Worksheet
Set sourceSheet = Worksheets("Data1")
Set outputSheet = Worksheets("Info2")
With sourceSheet
SourceLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With
With outputSheet
OutputLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
'This is the line where I keep getting the error:
.Range("G2:G" & OutputLastRow).Formula = _
"=VLOOKUP(A2,'" & sourceSheet & "'!$A$2:$B$" & SourceLastRow & ",2,0)"
End With
End Sub
您指的是工作表本身,而不是名稱。 嘗試:
.Range("G2:G" & OutputLastRow).Formula = _
"=VLOOKUP(A2,'" & sourceSheet.Name & "'!$A$2:$B$" & SourceLastRow & ",2,0)"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.