在VBA excel中设置VLOOKUP中的table_array

我正在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属性可以返回整个path和工作表名称。 给予太多不是有害的。

这可能是一个更好的方法。

  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