你可以使用ByRef
. 虽然可能不是最好的计划。
Sub Example(ByRef A As String, ByRef B As String)
A = A & "Hello"
B = B & "World!"
End Sub
Sub test()
Dim A As String
Dim B As String
A = "Test"
B = "Test"
Example A, B
Debug.Print A & " " & B
End Sub
编辑
如果您试图在工作表上提供 UDF,那么您可以完全无视我。
如果您从工作表中调用它(无论您的解决方案是什么),我认为您将始终对该函数进行多次 (2) 调用。假设它不会经常更改,您也许可以缓存函数的结果。它不会停止调用,但会停止一些多余的计算。
Private Cache As Object
Public Function MonsterFunction(ByVal A As Integer, ByVal B As Integer, Optional ByVal Add As Boolean = False) As Variant
Dim Key As String
Dim Result As Integer
Key = CStr(A) & IIf(Add, "+", "-") & CStr(B)
If Cache Is Nothing Then
Set Cache = CreateObject("Scripting.Dictionary")
End If
If Cache.Exists(Key) Then
MonsterFunction = Cache(Key)
Else
If Add Then
Result = A + B
Else
Result = A - B
End If
Cache.Add Key, Result
MonsterFunction = Result
End If
End Function