我在 VBA 中编写了一个函数,它将计算两个值。有什么方法可以让我从函数返回这两个值。
问问题
5801 次
1 回答
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
于 2013-11-08T11:29:50.063 回答