簡體   English   中英

具有范圍和2個工作簿的Excel Vlookup宏

[英]Excel Vlookup macro with range and 2 workbooks

我想做一個宏來從最后一行填充來運行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:\\聯[Roster_Iloilo.xlsx] ACTIVE“$ C:$ E。 參見文件圖片請在vlookup方面需要幫助。

這是您要嘗試的嗎? (未測試)

你可以寫你的公式

"=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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM