inserting variable range into vlookup formula

JP12345

New Member
Joined
Sep 11, 2018
Messages
9
Hi, Can anyone help with a the following:

This code works but seems long winded and I don't want to write 'rng' to a sheet cell, but don't know how to reference within the .formula line
Dim shtM As Worksheet
Dim LastRowP As Long
Dim LastRowM As Long
Dim rng As String


Set shtM = Worksheets("Merged Data")
LastRowM = shtM.Cells(shtM.Rows.Count, Range("A1").Column).End(xlUp).Row
LastRowP = Worksheets("Sheet1").Cells(Worksheets("Sheet1").Rows.Count, Range("A1").Column).End(xlUp).Row


rng = ("Sheet1!$A$3:$C$" & LastRowP)
Range("AR2").Value = rng
Range("AP2:AP" & LastRowM).Formula = "=IFERROR(VLOOKUP(Merged_Data[@[AD User Display Name]],INDIRECT($AR$2) ,2,FALSE),0)"

what I'd like is something along the lines of...
Range("AP2:AP" & LastRowM).Formula = "=IFERROR(VLOOKUP([@[AD User Display Name]],Sheet1!$A$3:$C$"" & LastRowP & "",3,FALSE),0)"

Where the bold text is the bit I can't get working. Thanks in advance
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
You should only have 1 set of " either side of the variable not two ""
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,201
Members
453,022
Latest member
RobertV1609

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