Vlookup in range of cells with LastcellRownumber

kelseye2eye

New Member
Joined
Apr 17, 2013
Messages
6
Hi,

I'm new to vba and I need a to run a vlookup on one worksheet based upon the data in another worksheet. The amount of data is variable in both worksheets.

Issues I am facing:
  • I want my Vlookup to populate from B2:Lastcellrownumber but I can't get it to work.
  • for the Vlookup, what is the best way to look up the range on the other worksheet when the data range is variable?

Code:
Dim WS As WorksheetDim Lastcell As Range
Dim LastcellRownumber As Long


Set WS = Worksheets("People")
With WS
    Set Lastcell = .Cells(.Rows.Count, "A").End(xlUp)
    LastcellRownumber = Lastcell.Row
    ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-1],'Users'![COLOR=#ff0000]here is where I need to be able to have a variable range based upon the amount of data in the 'Users' worksheet[/COLOR],2,0)"

End With

/CODE]

Thanks for any help!
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
You can get a formula version of last row:

=AGGREGATE(14,6,ROW(D:D)/(D:D<>""),1)

This will give the last row in column D that is not "" or Empty.
 
Upvote 0
Here Is The Line Of Code :
Code:
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-1],'Users'!R2C2:R" & Cells(Rows.Count, 2).End(xlUp).Row & "C2,2,0)"
ZAX
 
Upvote 0
Welcome to the board.

Here's how you write the formula to a variable range of cells...
Code:
Sub Test()
Dim Last_Row As Long


Application.ScreenUpdating = False


With Sheets("People")
    Last_Row = Range("A" & Rows.Count).End(xlUp).Row
    
    Range("B2:B" & Last_Row).Formula = "VLOOKUP..."
End With


Application.ScreenUpdating = False


End Sub

Here's how you create a dynamic range that you can refer to in your formula: Excel Names -- Excel Named Ranges
 
Upvote 0
Thanks everyone :) I ended up setting the last row of both sets of data as variables and it works perfectly in the vlookup!
 
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,340
Members
452,637
Latest member
Ezio2866

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top