我正在尝试在 VBA 中进行 vlookup。我正在引用同一工作簿中的另一张工作表,但我得到 #NAME?
因此。
Sub MatchNames()
l_row = Worksheets("Lookup").Cells(Rows.Count, 1).End(xlUp).Row
lookup_range = Worksheets("Lookup").Range("A2:E" & l_row)
Final_row = Cells(Rows.Count, 6).End(xlUp).Row
With Range("BG2:BG" & Final_row)
.FormulaR1C1 = "=vlookup(RC[-52],lookup_range, 2, False)"
End With
End Sub
最佳答案
尝试,
l_row = Worksheets("Lookup").Cells(Rows.Count, 1).End(xlUp).Row
Set lookup_range = Worksheets("Lookup").Range("A2:E" & l_row) '<~~one edit here
With Range("BG2:BG" & Final_row)
.FormulaR1C1 = "=vlookup(RC[-52]," & lookup_range.address(referencestyle:=xlR1C1, external:=true) & ", 2, False)" '<~~another edit here
End With
Range.Address property可以返回整个路径和工作表名称。付出太多并没有坏处。
这可能是一个更好的方法。
Dim l_row As Long, f_row As Long, sLookup_Range As String
With Worksheets("Lookup")
l_row = .Cells(Rows.Count, "A").End(xlUp).Row
sLookup_Range = .Range("A2:E" & l_row).Address(ReferenceStyle:=xlR1C1, external:=True)
End With
With Worksheets("Sheet1")
f_row = .Cells(Rows.Count, "G").End(xlUp).Row
With .Range("BG2:BG" & f_row)
.FormulaR1C1 = "=vlookup(RC[-52]," & sLookup_Range & ", 2, False)"
End With
End With
关于excel - 在 VBA excel 中的 VLOOKUP 中设置 table_array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35474314/