我有两张纸,Sht1和sht2。使用sht1,我总是查看列B并检查ID是否与sht2的列B匹配,如果它们匹配,那么我将sht1的列Q值复制到sht2。
我尝试通过以下代码完成,但我无法获得结果,代码中没有显示错误。
我丢失了我的代码错误。任何人都可以帮我摆脱这个。
Sub vlookupComments()
Dim totalrows As Long, totalrowsSht2 As Long
Dim ws As Worksheet
totalrows = Sheets("sht1").Cells(Rows.Count, "A").End(xlUp).Row
totalrowsSht2 = Sheets("sht2").Cells(Rows.Count, "A").End(xlUp).Row
Sheets("sht1").Range("Q5:Q" & totalrows).Formula = Application.WorksheetFunction.IfError(Application.VLookup(Sheets("sht1").Range("B5:B" & totalrowsSht2), Sheets("sht2").Range("$A:$Q"), 17, 0), "")
End Sub
答案 0 :(得分:1)
你的问题是“将sht1的列Q值复制到sht2”,但公式却是另一种方式。我选择了公式,
尝试下面的宏,
Sub vlookupComments()
Dim totalrows As Long, totalrowsSht2 As Long
Dim ws As Worksheet
totalrows = Sheets("sht1").Cells(Rows.Count, "A").End(xlUp).Row
totalrowsSht2 = Sheets("sht2").Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To totalrows
Sheets("sht1").Cells(i, "Q").Value = _
Application.WorksheetFunction.VLookup(Sheets("sht1").Cells(i, "B"), _
Sheets("sht2").Range("A1:Q" & totalrowsSht2), 17, 0)
Next i
End Sub
选项2:
Sub lookupmaturitystart()
Dim totalrows As Long, totalrowsSht2 As Long
totalrows = Sheets("Maturity_BU").Cells(Rows.Count, "A").End(xlUp).Row
totalrowsSht2 = Sheets("Maturity_Lastweek").Cells(Rows.Count, "A").End(xlUp).Row
For i = 5 To totalrows
For j = 1 To totalrowsSht2
If Sheets("Maturity_BU").Cells(i, "B") = Sheets("Maturity_Lastweek").Cells(j, "B") Then
Sheets("Maturity_BU").Cells(i, "Q") = Sheets("Maturity_Lastweek").Cells(j, "Q")
End If
Next j
Next i
End Sub
答案 1 :(得分:0)
尝试以下代码
Sheets("sht1").Range("Q5:Q" & totalrows).Value = "=IFERROR(VLOOKUP(B5,Sheet2!$A:$Q,17,0),"""")"