VBA code to create vlookup formula in range

RoccoM

New Member
Joined
Apr 22, 2019
Messages
19
Guys:
I have to create a vlookup for a range of cells. I have included an example below.
Here is my problem. How do I account for the formula when I do not know how many rows there will be in my range?
I already created a variable to store my row count, not sure on the rest.
Example: here is my current lookup formula that I created manually. ( obviously I know the range): =VLOOKUP(AE2,$AC$2:$AD$64828,2,FALSE

I copied my data into the same sheet so I can count the rows and store it into a variable. I just need help with using variable in my lookup formula; or is there an easier way?


Example data:
[TABLE="width: 839"]
<tbody>[TR]
[TD]RCD:TYPE[/TD]
[TD]RCD:FUNCTION[/TD]
[TD]Create From[/TD]
[TD]Lookup: Function Field[/TD]
[/TR]
[TR]
[TD]3C16942A[/TD]
[TD]SWITCH[/TD]
[TD]BLANK-PNL-19INCH-2[/TD]
[TD]CABINET_SUPPLY[/TD]
[/TR]
[TR]
[TD]3C17111[/TD]
[TD]ETH_SWITCHMODULE[/TD]
[TD]BLANK-PNL-19INCH-2[/TD]
[TD]CABINET_SUPPLY[/TD]
[/TR]
[TR]
[TD]3C17121[/TD]
[TD]ETH_SWITCHMODULE[/TD]
[TD]BLANK-PNL-19INCH-2[/TD]
[TD]CABINET_SUPPLY[/TD]
[/TR]
[TR]
[TD]3C17131[/TD]
[TD]ETH_SWITCHMODULE[/TD]
[TD]BLANK-PNL-19INCH-2[/TD]
[TD]CABINET_SUPPLY[/TD]
[/TR]
[TR]
[TD]3C17203[/TD]
[TD]SWITCH[/TD]
[TD]BLANK-PNL-19INCH-1[/TD]
[TD]CABINET_SUPPLY[/TD]
[/TR]
[TR]
[TD]3C17203[/TD]
[TD]SWITCH[/TD]
[TD]BLANK-PNL-19INCH-1[/TD]
[TD]CABINET_SUPPLY[/TD]
[/TR]
</tbody>[/TABLE]


Thank you,
Rocco
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
How about
Code:
Sub RoccoM()
   Dim UsdRws As Long
   UsdRws = Range("AE" & Rows.Count).End(xlUp).Row
   Range("AG2:AG" & UsdRws).Formula = "=vlookup(AE2,$AC$2:$AD$" & UsdRws & ",2,0)"
End Sub
 
Last edited:
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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