微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用VB / VBA中的“CallByName”调用模块中包含的Sub或Function

使用CallByName调用classModule中的函数很容易
标准模块内的功能怎么样?
''#inside class module
''#classModule name: clsExample
  Function classFunc1()
     MsgBox "I'm class module 1"
  End Function
''# 
''#inside standard module
''#Module name: module1
  Function Func1()
     MsgBox "I'm standard module 1"
  End Function
''#
''# The main sub
Sub Main()
''# to call function inside class module
dim clsObj as New clsExample
Call CallByName(clsObj,"ClassFunc1")

''# here's the question... how to call a function inside a standard module
''# how to declare the object "stdobj" in reference to module1?
Call CallByName(stdobj,"Func1") ''# is this correct?

End Sub
我认为jtolle的回答最好地解决了这个问题 – 对Application.Run的小参考可能就是答案.提问者不想简单地使用func1或Module1.func1 – 首先想要使用CallByName的原因是在编译时不知道所需的function.sub名称.在这种情况下,Application.Run确实有效,例如:
Dim ModuleName As String
Dim FuncName As String
Module1Name = "Module1"
FuncName = "func1"
Application.Run ModuleName & "." & FuncName

您还可以在ModuleName之前添加项目名称,并添加一个句点“.”.不幸的是,Application.Run不会返回任何值,因此虽然您可以调用函数,但您将无法获得其返回值.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐