在不同的工作簿模块中传递和 return 变量

Pass and return Variable in different Workbook Module

我无法 return 从另一个工作簿中的不同模块更改变量。

练习册 A:

Dim A As String
Dim B As String
Dim C As String
Dim D As String

A = "A"
B = "B"
C = "C"

 Workbooks.Open FileName:= _
        "C:\Users\two.xlsm"

Application.Run "'two.xlsm'!test_her", A, B, C, D

Debug.Print A, B, C, D

End Sub

练习册 B:

Public B
Public D

Public Sub test_her(ByRef A As String, ByVal B As String, C As String, ByRef D As String)


A = A & "TEST"
B = B & "TEST"
C = C & "TEST"
D = "CHANGED"

End Sub

可以将变量传递给“test_her”并进行处理。但是我还没有弄清楚如何将它们传回修改过的?

如果我在同一个工作簿中工作,它会使用 public 变量,只有在不同的工作簿中我才能传递它们。

快速搜索后,看起来解决方案是将 Application 对象分配给一个对象变量,然后从该变量调用 运行 方法。

Sub test()

Dim A As String
Dim B As String
Dim C As String
Dim D As String

A = "A"
B = "B"
C = "C"

 Workbooks.Open Filename:= _
        "C:\Users\two.xlsm"
        
Dim xlApp As Object
Set xlApp = Application

xlApp.Run "'two.xlsm'!test_her", A, B, C, D

End Sub