如何在Vlookup中调用列索引号:VBA

时间:2017-07-05 08:33:01

标签: excel vba excel-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

1 个答案:

答案 0 :(得分:1)

您需要取出d之外的"变量。

替换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,而da将定义为Variant。< / p>

将此行更改为Dim d As Long, a As Long, Lastrow As Long