Dynamic VBA program

Helge Larsen

New Member
Joined
Sep 22, 2008
Messages
19
I need some VBA like this::)

Dim AllSubs(1 To 3) As String

Sub myMAIN()
AllSubs(1) = "Sub1"
AllSubs(2) = "Sub2"
AllSubs(3) = "Sub3"
Call Call_VBA(2) ' This should call Sub2.​
End Sub

Sub Call_VBA(ByRef I As Integer)
Dim mySub As String
mySub = AllSubs(I)
Call mySub ' Of course this will not function!​
End Sub

Sub Sub1(): MsgBox "Sub1": End Sub
Sub Sub2(): MsgBox "Sub2": End Sub
Sub Sub3(): MsgBox "Sub3": End Sub


Is it at all possible to make a such 'dynamic' call of a VBA routine.:rolleyes:
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Hi,
This should do what you want:
Code:
Dim AllSubs(1 To 3) As String

Sub myMAIN()

    AllSubs(1) = "Sub1"
    AllSubs(2) = "Sub2"
    AllSubs(3) = "Sub3"
    Call Call_VBA(2) ' This should call Sub2.

End Sub

Sub Call_VBA(ByRef I As Integer)

    Dim mySub As String
    mySub = AllSubs(I)
    [COLOR="Blue"]Application.Run ThisWorkbook.Name & "!" &  mySub[/COLOR]
    
End Sub

Sub Sub1(): MsgBox "Sub1": End Sub
Sub Sub2(): MsgBox "Sub2": End Sub
Sub Sub3(): MsgBox "Sub3": End Sub

If the subs are all in the same standard module (or I think even in the same workbook in standard modules) you can use just:
Application.Run mySub
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,334
Members
452,907
Latest member
Roland Deschain

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