Calling a Sub or Function contained in a module using “CallByName” in VB/VBA

前端 未结 5 884
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-04 18:25

It is easy to call a function inside a classModule using CallByName How about functions inside standard module?

\'\'#inside class module
\'\'#classModule nam         


        
5条回答
  •  醉酒成梦
    2021-01-04 18:56

    I think jtolle's response addressed the question best - the small reference to Application.Run may be the answer. The questioner doesn't want to use simply func1 or Module1.func1 - the reason one would want to use CallByName in the first place is that the desired function.sub name is not known at compile time. In this case, Application.Run does work, e.g.:

    Dim ModuleName As String
    Dim FuncName As String
    Module1Name = "Module1"
    FuncName = "func1"
    Application.Run ModuleName & "." & FuncName
    

    You can also prepend the Project Name before the ModuleName and add another period ".". Unfortunately, Application.Run does not return any values, so while you can call a function, you won't get its return value.

提交回复
热议问题