首页 > 解决方案 > 将 Sub 作为参数传递

问题描述

我有一个子

Sub MySub1

MsgBox "I am ok!"

End Sub

然后我有一个带有参数的所谓“方法子”

Sub MySub2 (Parameter1, Parameter2, MethodName)

MsgBox Parameter1
MsgBox Parameter2

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

Dim 

这给出了预期值或函数的错误。如何使这项工作?

标签: excelvba

解决方案


您需要创建Module(如下面的屏幕截图所示):

在此处输入图像描述

然后粘贴这段代码:

    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

然后单击Run Sub/User Form按钮(或单击F5键)运行您的宏。


推荐阅读