如何在Vlookup中调用列索引号:VBA
如何在vlookup中调用列索引no d
。 "=vlookup(RC[-15],"& Rg.Address(True, True, xlR1C1, xlExternal) & ",d,False)"
是否正确? 请参考下面的代码(注意Vlookup使用一个工作簿到另一个工作簿)
Sub Vlkuprangcall() Dim strColumn As String Dim Rg As Range Dim wb2 As Workbook Dim d, a, Lastrow As Long Set wb2 = Workbooks.Open("C:\Users\ashokkumar.d\Desktop\Test\IC Raphael\Janalakshmi\MIS\MIS.xlsx") Set Rg = wb2.Sheets(3).Range("A3:Z10000") d = 6 Application.Workbooks(2).Activate With ActiveSheet a = ActiveCell.Column Lastrow = 100 strColumn = Split(ActiveCell.Address, "$")(1) ActiveCell.FormulaR1C1 = "=vlookup(RC[-15],"& Rg.Address(True, True, xlR1C1, xlExternal) & ",d,False)" ActiveCell.AutoFill Destination:=Range(ActiveCell, Range(strColumn & Lastrow)) End With End Sub
你需要把“ d
variables取出来。
replace您的FormulaR1C1
线:
ActiveCell.FormulaR1C1 = "=vlookup(RC[-15],"& Rg.Address(True, True, xlR1C1, xlExternal) & ",d,False)"
带有:
ActiveCell.FormulaR1C1 = "=vlookup(RC[-15]," & Rg.Address(True, True, xlR1C1, xlExternal) & "," & d & ",False)"
注意 :定义Dim d, a, Lastrow As Long
,仅意味着Lastrow As Long
,而d
和a
将被定义为Variant
。
将此行更改为Dim d As Long, a As Long, Lastrow As Long