Sub 过程的参数有两种传递方式:按值传递(ByVal)和按地址传递(ByRef)。如下例: Sub password (ByVal x as integer, ByRef y as integer) If y=100 then y=x+y else y=x-y x=x+100 End sub
Sub call_password () Dim x1 as integer Dim y1 as integer x1=12 y1=100 Call password (x1,y1) ‘调用过程方式:1. Call 过程名(参数1, 参数2…) ; 2. 过程名 参数1, 参数2… debug.print x1,y1 ‘结果是12、112,y1按地址传递改变了值,而x1按值传递,未改变原值 End sub
Function password(ByVal x as integer, byref y as integer) as boolean If y=100 then y=x+y else y=x-y x=x+100 if y=150 then password=true else password=false End Function
Sub call_password () Dim x1 as integer Dim y1 as integer x1=12 y1=100 if password then ‘调用函数:1. 作为一个表达式放在=右端 ; 2. 作为参数使用 debug.print x1 end if End sub