entering a vlookup formula into a cell using vba

Brenton Hombsch

New Member
Joined
May 5, 2019
Messages
6
i am having trouble with the below code.

i am trying to compare a cells text "Week1_Program" is the name of the cell
with a list of that i have named each cell "WeekType1","WeekType2" etc
and if they are the same return a range name eg"WeekType1Range","WeekType2Range"etc

i then want to use this result to put into a vlookup formula to enter onto a worksheet

Hope this makes some sense

sub LookupRangeName()

Dim R As String

If Worksheets("Programs").Range("Week1_Program") = Worksheets("Programs").Range("WeekType1") Then
R = "WeekType1Range"

ElseIf Worksheets("Programs").Range("Week1_Program") = Worksheets("Programs").Range("WeekType2") Then
R = "WeekType2Range"

ElseIf Worksheets("Programs").Range("Week1_Program") = Worksheets("Programs").Range("WeekType3") Then
R = "WeekType3Range"


Endif

Range("C4").Formula = "=VLOOKUP($B4,R,2,FALSE)"


end sub

Thanks in advance

 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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