Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call a Sub with a String

Tags:

string

excel

vba

I want to call a different sub depending on the value of i.

For example, if i = 1 call sale_call1 and if i = 2 call sale_call2.

Private Sub test_Click()
    Dim i As String
    Dim pro As String

    i = Me.tb1.Value
    pro = "sale_call" + i

    If i = "1" Then
        Call pro
    Else
        Call pro
    End If
End Sub

Sub sale_call1()
    MsgBox "Hello"
End Sub

Sub sale_call2()
    MsgBox "goodbye"
End Sub
like image 818
CallMeMunchy Avatar asked Apr 12 '13 11:04

CallMeMunchy


People also ask

Can you call a sub within a sub?

You can only call a Private Sub from another Sub if they two are in the same module of your VBA window.

Can you call a sub in VBA?

To call a Sub procedure from another procedure, type the name of the procedure and include values for any required arguments. The Call statement is not required, but if you use it, you must enclose any arguments in parentheses. Use a Sub procedure to organize other procedures so they are easier to understand and debug.

How do you call a sub in another module?

To call a macro or function that is in the same workbook (it need not be in the same module) just type the name of the macro/function and any arguments it requires on one line of the calling macro. Another way is to prefix the macro/function called with the word Call.

How do you call another module in VBA?

VBA Example: Run Another Macro from a Macro Just type the word Call then space, then type the name of the macro to be called (run).


2 Answers

Try this

Replace Call pro with Application.Run pro

Example

Private Sub test_Click()
    Dim i As String
    Dim pro As String

    i = 1
    pro = "sale_call" + i

    '~~> This will run sale_call1
    Application.Run pro

    i = 2
    pro = "sale_call" + i

    '~~> This will run sale_call2
    Application.Run pro
End Sub

Sub sale_call1()
    MsgBox "Hello"
End Sub

Sub sale_call2()
    MsgBox "goodbye"
End Sub

FOLLOWUP

If your code is not in a module but in a Userform or Sheet Code area then Application.Run will not work till the time sale_call1 or sale_call2 is not placed in a module. If you do not wish to move them to a module then you will have to use CallByName. Check Excel's inbuilt help on this function. Here is an example which assumes that the code is in Userform1

Private Sub CommandButton1_Click()
    Dim i As String
    Dim pro As String

    i = 1
    pro = "sale_call" + i

    '~~> This will run sale_call1
    CallByName UserForm1, pro, VbMethod

    i = 2
    pro = "sale_call" + i

    '~~> This will run sale_call2
    CallByName UserForm1, pro, VbMethod
End Sub

Sub sale_call1()
    MsgBox "Hello"
End Sub

Sub sale_call2()
    MsgBox "goodbye"
End Sub
like image 124
Siddharth Rout Avatar answered Oct 09 '22 13:10

Siddharth Rout


Just add as prefix the workbook name where the macro is hosted. Like when doing a formula in a cell:

Application.Run "WorkbookNameAsString.app_ext!MacroName"
like image 45
Maiqui Avatar answered Oct 09 '22 14:10

Maiqui