原文: http://blog.csdn.net/testing_is_believing
第一种方法是利用全局变量,因此根本不用返回,只要直接给全局变量赋值即可。
例:
Dim intNumber_1: intNumber_1 = 40
Dim intNumber_2: intNumber_2 = 80
Public Sub PassValues
intNumber_1 = intNumber_1/4
intNumber_2 = intNumber_2/4
End Sub
PassValues
MsgBox "intNumber_1 = " & intNumber_1 &_
vbLf & "intNumber_2 = " & intNumber_2
第二种方法是用返回一个Dictionary集合的方式来返回多个值。
例:
Public Function PassValues(ByVal Num_1, ByVal Num_2)
Set oDict = CreateObject( "Scripting.Dictionary" )
With oDict
.Add "Num_1", Num_1/4
.Add "Num_2", Num_2/2
End With
Set PassValues = oDict
End Function
Set colNumbers = PassValues(40,80)
MsgBox "intNumber_1 = " & colNumbers.Item("Num_1") &_
vbLf & "intNumber_2 = " & colNumbers.Item("Num_2")
第三种方法是用返回一个数组的方式来返回多个值。
例:
Public Function PassValues(ByVal Num_1, ByVal Num_2)
Dim arrArray: ReDim arrArray(2)
arrArray(0) = Num_1/4
arrArray(1) = Num_2/2
PassValues = arrArray
End Function
arrNew = PassValues(40,80)
MsgBox "intNumber_1 = " & arrNew(0) &_
vbLf & "intNumber_2 = " & arrNew(1)
第四种方法是将需要返回的值连接到一个字符串来返回,这种方法要求接收数据的调用者需要用Split来切割出一个个数据。
例:
Public Function PassValues(ByVal Num_1, ByVal Num_2)
Num_1 = Num_1/4
Num_2 = Num_2/2
PassValues = Num_1 & "," & Num_2
End Function
sNum = PassValues(40,80)
MsgBox "intNumber_1 = " & Split(sNum, ",")(0) &_
vbLf & "intNumber_2 = " & Split(sNum, ",")(1)
第五种方法是通过ByRef关键字指定参数为引用,这种方法与用全局变量的方法类似,也是直接修改值,不同返回。
例:
Dim intNumber_1: intNumber_1 = 40
Dim intNumber_2: intNumber_2 = 80
Public Sub PassValues(ByRef Num_1, ByRef Num_2)
Num_1 = Num_1/4
Num_2 = Num_2/2
End Sub
PassValues intNumber_1, intNumber_2
MsgBox "intNumber_1 = " & intNumber_1 &_
vbLf & "intNumber_2 = " & intNumber_2