Excel Vlookup宏与范围和2个工作簿

时间:2015-05-23 07:29:09

标签: excel vba excel-vba

我想创建一个宏来从最后一行填充vlookup。 下面的代码是获取要填充的最后一行(列J)和最后一行填充(列A),以下公式是获取此2列的最后一行;

Sub lookup()
'Find the last Row with data in a Column
'In this example we are finding the last row of column A (Filled) and J (to be filled)
    Dim lastRowA As Long
    Dim lastRowJ As Long
    With ActiveSheet
        lastRowA = .Cells(.Rows.Count, "A").End(xlUp).Row
        lastRowJ = .Cells(.Rows.Count, "J").End(xlUp).Row
    End With
    MsgBox lastRowA & "   " & lastRowJ

End Sub

vlookup 会在 C 列中查找值,并查看另一个Excel文件中的值 C:\ LINKED [Roster_Iloilo.xlsx] ACTIVE&#39 !$ C:$ E 。见File的图片 需要vlookup的帮助。

1 个答案:

答案 0 :(得分:0)

这是你在尝试什么? (未测试)

您可以编写公式

"=vlookup(C40846,'C:\LINKED[Roster_Iloilo.xlsx]ACTIVE'!$C:$E,3,0)"

作为

"=vlookup(C" & "40846" & ",'C:\LINKED[Roster_Iloilo.xlsx]ACTIVE'!$C:$E,3,0)"

所以你要做的就是替换最后一行:)

Sub Sample()
    Dim ws As Worksheet
    Dim lastRowA As Long
    Dim sFormulaPre As String
    Dim sFormulaSuff As String

    Set ws = ThisWorkbook.Sheets("Sheet1")

    '=vlookup(C40846,'C:\LINKED[Roster_Iloilo.xlsx]ACTIVE'!$C:$E,3,0)
    sFormulaPre = "=vlookup(C"
    sFormulaSuff = ",'C:\LINKED[Roster_Iloilo.xlsx]ACTIVE'!$C:$E,3,0)"

    With ws
        lastRowA = .Cells(.Rows.Count, "A").End(xlUp).Row

        MsgBox sFormulaPre & lastRowA & sFormulaSuff
        '~~> Usage
        '.Cells(1, 1).Formula = sFormulaPre & lastRowA & sFormulaSuff
    End With
End Sub