ChiHam
ChiHam

Reputation: 85

Excel Vlookup macro with range and 2 workbooks

I would like a to make a macro to run vlookup from the last row filled. 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;

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. See picture of the File Will need help with the vlookup please.

Upvotes: 1

Views: 255

Answers (1)

Siddharth Rout
Siddharth Rout

Reputation: 149315

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

Upvotes: 0

Related Questions