我只是想知道这里发生了什么,我做错了什么?
案例1:
当仅传递Range对象作为参数抛出时 - 对象需要错误
示例代码:
Dim ws As Worksheet
Dim rng As Range
Set ws = Worksheets("Project")
ws.Activate
Set rng = Range("A1:A12")
passRange(rng) ' Throws Runtime error
End Sub
Function passRange(rnge As Range)
MsgBox rnge.count
End Function
案例2,
当传递2个参数时,它可以正常工作。
示例代码:
Dim ws As Worksheet
Dim rng As Range
Dim IL As Long
IL = 12
Set ws = Worksheets("Project")
ws.Activate
Set rng = Range("A1:A12")
Call passRange(rng, IL)
End Sub
Function passRange(rnge As Range, IL As Long)
MsgBox rnge.count
End Function
答案 0 :(得分:1)
调用过程时不需要使用Call关键字。但是,如果使用Call关键字调用需要参数的过程,则必须将参数列表括在括号中。如果省略过程调用中的Call关键字,则还必须省略argumentlist周围的括号。
请参阅here
代码如何使用ist
Sub testSub(rg As Range)
End Sub
Sub TestItA()
Dim rg As Range
Set rg = Range("A1")
testSub rg
Call testSub(rg)
testSub (rg) 'will throw a run time error
call testsub rg ' will even not compile
End Sub
Function testFunc(rg As Range)
End Function
Sub TestIt()
Dim rg As Range
Set rg = Range("A1")
testFunc rg
Call testFunc(rg)
testFunc (rg) 'will throw a run time error
call testfunc rg ' will even not compile
End Sub
答案 1 :(得分:0)
如果您希望能够将一个或两个参数传递给该函数,则应该像这样重写:
Function passRange(rnge As Range, Optional IL As Long)
MsgBox rnge.count
End Function
否则,当只传递一个参数时,它会抛出一个exeption。或者,您可以使两个参数都是可选的。