我在VBA中编写了一个函数,它将计算两个值。有什么方法可以从函数中返回这两个值。
答案 0 :(得分:2)
在VBA中您可以通过两种方式返回更多值:返回数组或使用ByRef声明,请参阅:
'return as array
Function ReturnTwoValues(x As Long) As Long()
Dim ret(1) As Long
ret(0) = x
ret(1) = x * 2
ReturnTwoValues = ret
End Function
'return parameters declared as ByRef
Sub ReturnTwoPars(x As Long, ByRef y1 As Long, ByRef y2 As Long)
y1 = x
y2 = x * 2
End Sub
Sub Test()
'return an array
Dim ar() As Long
ar = ReturnTwoValues(2) 'variable must be the same type as function
Debug.Print ar(0), ar(1)
'use ByRef
Dim y1 As Long
Dim y2 As Long
ReturnTwoPars 2, y1, y2
Debug.Print y1, y2
End Sub