Using a String as a Sub name

Moosles

New Member
Joined
Apr 1, 2021
Messages
23
Office Version
  1. 2019
Platform
  1. Windows
I have a series of Subroutines that are all similarly named but different enough that they need to be treated distinctly. There is a dropdown menu on a named range called LeadOption which has 5 possible selections (numbers 1-5), and depending on which option is chosen I want a particular Sub to run as part of a larger macro.

For example if LeadOption is 1, then I want the Sub Example_1 to run. If LeadOption is 2 then Example_2, and so on. I tried the code:

VBA Code:
Dim SubOption as String
SubOption =  "Example_" & Range("LeadOption").value

Call SubOption

But this breaks before running, with the error Compile Error: Expected Sub, Function or Property. Ideally I don't want to have to write out a load of IF statements, as I will have to do this for another 20+ LeadOption style named ranges that are doing similar things - is there a way to use a String to selectively call a Sub which is dependent on an input like this?
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
You can do it with "Application.Run", i.e.
VBA Code:
Application.Run SubOption
 
Upvote 0
Solution

Forum statistics

Threads
1,224,816
Messages
6,181,141
Members
453,021
Latest member
Justyna P

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