'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("''!module2.f1", 1) 'ok
End Sub
Sub test6()
Application.Run "''!module2.s1" 'ok
End Sub
Sub test7()
Application.Run ("'C:\Users\user\Desktop\3\'!module2.s1") 'ok
End Sub
Sub test8()
f1r = Application.Run("'C:\Users\user\Desktop\3\'!module2.f1", 1) 'ok
End Sub
'-->module1
Sub s1()
MsgBox "s1"
End Sub
'-->module2
Sub s1()
MsgBox "m2s1"
End Sub
Function f1(a)
f1 = a
MsgBox f1
End Function
调用方法:
- 方法名,参数1,参数2,xxx
- (方法名,参数1,参数2,xxx)
两种方法都可以,如果要返回值只能调用第二种,即result= (方法名,参数1,参数2,xxx)
同一个excel文件的方法调用:
module3/4/5在同一个excel文件。module5利用调用其他module的sub/function时,如果该sub/function名在所有module里唯一,可以不加模块名(推荐全加模块名)。否则报错,需要模块名.方法名调用。
模块名和方法名都不区分大小写。
不同excel文件的方法调用:
- 方法1: " ‘文件名’ “!模块名.方法名”
- 方法2: " ‘路径+文件名’ “!模块名.方法名”
必须带上文件名和模块名,即使方法在该文件唯一。且文件名两边有单引号,文件名和模块名间有!。
如果被调用的工作簿是打开状态,方法1和方法2都可以。如果是关闭状态,方法2会打开路径+文件名进行调用,方法1会打开当前文件路径+文件名进行调用。