Variable in macro name

ChristineJ

Well-known Member
Joined
May 18, 2009
Messages
761
Office Version
  1. 365
Platform
  1. Windows
There are four macros named Macro1, Macro2, Macro3, and Macro4. Any of these may be called within another macro.

I'd like the number in those four macro names to be a variable and dependent on the value in cell A3.

For example, if the value in A3 is 3, Macro3 would be called. If that value in A3 is 1, Macro1 would be called.

Can this be done? Thanks!
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Try it this way
VBA Code:
Sub MM1()
Select Case Range("A3").Value
Case Is = 1
    Call Macro1
Case Is = 2
    Call Macro2
Case Is = 3
    Call Macro3
Case Is = 4
    Call Macro4
 End Select
End Sub
 
Upvote 0
The Select Case is what I am currently using, as you have shown.

However, since ultimately I will have over 100 possible macros to call based on the value in A3, I was hoping to be able to consolidate significantly by making the number in the macro name a variable.

Thanks for your reply.
 
Upvote 0
How about this :
VBA Code:
Sub Test()

    On Error GoTo errHandler
        Application.Run "Macro" & [a3]
    On Error GoTo 0
    
errHandler:
        If Err.Number = 1004 Then
            MsgBox Err.Description, vbCritical, "Error!"
        End If
    
End Sub
 
Upvote 0
Awesome, Jaafar Tribak! It works perfectly and is exactly what I needed. Thanks so much for you help.
 
Upvote 0

Forum statistics

Threads
1,214,987
Messages
6,122,614
Members
449,090
Latest member
vivek chauhan

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