简体   繁体   English

具有范围和2个工作簿的Excel Vlookup宏

[英]Excel Vlookup macro with range and 2 workbooks

I would like a to make a macro to run vlookup from the last row filled. 我想做一个宏来从最后一行填充来运行vlookup。 The following code is to get the last row to be filled (column J) and the last row filled (column A), the following formula is to get the last rows of this 2 columns; 下面的代码将获取要填充的最后一行(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

The vlookup looks for the value in column C and look in the range in another excel file C:\\LINKED[Roster_Iloilo.xlsx]ACTIVE'!$C:$E . VLOOKUP会在C列的值,并期待在范围内的另一个excel文件C:\\联[Roster_Iloilo.xlsx] ACTIVE“$ C:$ E。 See picture of the File Will need help with the vlookup please. 参见文件图片请在vlookup方面需要帮助。

Is this what you are trying? 这是您要尝试的吗? (Untested) (未测试)

You can write your formula 你可以写你的公式

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

as

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

So all you have to do is replace the last row :) 因此,您要做的就是替换最后一行:)

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