vba的Application.Run

28 篇文章 2 订阅
'thisworkbook.module3
Sub Macro1()
    MsgBox 1
End Sub

Sub Macro2()
    MsgBox 2
    Application.Run "Macro1"
End Sub

Function f1(a)
    MsgBox a & "module3"
    f1 = a
End Function
'thisworkbook.module4
Sub maCRO2()
  Application.Run ("Macro1")
End Sub

Function f1()
    f1 = "module4"
    MsgBox f1
End Function
'thisworkbook.module5
Sub test1()
   Application.Run "Macro2"				'ng
End Sub

Sub test2()
   Application.Run("module3.Macro2")	'ok
End Sub

Sub test3()
   f1r = Application.Run "module4.f1"	'ok
End Sub

Sub test4()
   f1r = Application.Run("module3.f1", 1)	'ok
End Sub

Sub test5()
   f1r = Application.Run("'2.xlsm'!module2.f1", 1)	'ok
End Sub

Sub test6()
   Application.Run "'2.xlsm'!module2.s1"	'ok
End Sub

Sub test7()
   Application.Run ("'C:\Users\user\Desktop\3\2.xlsm'!module2.s1")	'ok
End Sub

Sub test8()
   f1r = Application.Run("'C:\Users\user\Desktop\3\2.xlsm'!module2.f1", 1)	'ok
End Sub
'2.xlsm-->module1
Sub s1()
    MsgBox "s1"
End Sub
'2.xlsm-->module2
Sub s1()
    MsgBox "m2s1"
End Sub

Function f1(a)
    f1 = a
    MsgBox f1
End Function

调用方法:

  • application.run 方法名,参数1,参数2,xxx
  • application.run (方法名,参数1,参数2,xxx)

两种方法都可以,如果要返回值只能调用第二种,即result=application.run (方法名,参数1,参数2,xxx)

同一个excel文件的方法调用:

module3/4/5在同一个excel文件。module5利用application.run调用其他module的sub/function时,如果该sub/function名在所有module里唯一,可以不加模块名(推荐全加模块名)。否则报错,需要模块名.方法名调用。
模块名和方法名都不区分大小写。

不同excel文件的方法调用:

  • 方法1:Application.Run " ‘文件名’ “!模块名.方法名”
  • 方法2:Application.Run " ‘路径+文件名’ “!模块名.方法名”
    必须带上文件名和模块名,即使方法在该文件唯一。且文件名两边有单引号,文件名和模块名间有!。
    如果被调用的工作簿是打开状态,方法1和方法2都可以。如果是关闭状态,方法2会打开路径+文件名进行调用,方法1会打开当前文件路径+文件名进行调用。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值