加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-17 00:21:39 所属栏目:大数据 来源:网络整理
导读:使用CallByName调用classModule中的函数很容易 标准模块内的功能怎么样? ''#inside class module''#classModule name: clsExample Function classFunc1() MsgBox "I'm class module 1" End Function''# ''#inside standard module''#Module name: module1 F
使用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不会返回任何值,因此虽然您可以调用函数,但您将无法获得其返回值.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读