10101
10101

Reputation: 2402

Pass Sub as a parameter

I have a sub

Sub MySub1

MsgBox "I am ok!"

End Sub

Then I have a so called "method sub" with parameters

Sub MySub2 (Parameter1, Parameter2, MethodName)

MsgBox Parameter1
MsgBox Parameter2

MethodName

End Sub

Then I would like to run this whole chain in my master. I have tried the following:

Sub MasterSub

Dim Parameter1 As String
Dim Parameter2 As String
Dim MethodName As String

Parameter1 = "Ou"
Parameter2 = "Yes"
MethodName = MySub1

MySub2 Parameter1, Parameter2, MethodName

Dim 

This is giving an error that value or function is expected. How to make this work?

Upvotes: 1

Views: 256

Answers (1)

timnavigate
timnavigate

Reputation: 739

You need to create Module (as you can see on screenshot below):

enter image description here

Then paste this code:

    Private Sub MySub1()
        MsgBox "I am ok!"
    End Sub

    Private Sub MySub2(Parameter1, Parameter2, MethodName)
        MsgBox Parameter1
        MsgBox Parameter2

        Application.Run MethodName
    End Sub

    Sub MasterSub()
        Dim Parameter1 As String
        Dim Parameter2 As String
        Dim MethodName As String

        Parameter1 = "Ou"
        Parameter2 = "Yes"
        MethodName = "MySub1"

        MySub2 Parameter1, Parameter2, MethodName
    End Sub

And then click on Run Sub/User Form button (or click on F5 key) for run your macro.

Upvotes: 1

Related Questions