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

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

Forum statistics

Threads
1,215,063
Messages
6,122,934
Members
449,094
Latest member
teemeren

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