Run a sub-routine within another sub-routine - Compile Error: Argument not optional
Run a sub-routine within another sub-routine - Compile Error: Argument not optional
Sub StoreQA(ShapeClicked As Shape)
MsgBox "yeee"
End Sub
Sub ThisIsToBeRun()
StoreQA
End Sub
我收到编译错误:参数不是可选的。
我尝试提及 StoreQA(ShapeClicked As Shape)
,但这也无济于事。
Argument not optional
您在这里定义了一个参数,名称为ShapeClicked
:
Sub StoreQA(ShapeClicked As Shape)
因为参数不是 Optional
,任何代码都必须 为这个参数提供参数 来调用这个 StoreQA
过程:
' gets the first Shape in Sheet1 (assumes it exists), then passes it to StoreQA:
StoreQA Sheet1.Shapes(1)
如果不需要该参数,请将其删除:
Sub StoreQA()
如果有的地方需要,有的地方不需要,可以考虑制作Optional
:
Sub StoreQA(Optional ByVal ShapeClicked As Shape)
那么您可以在不提供任何参数的情况下合法地调用该过程:
StoreQA '<~ compiles fine now
可选注意事项
如果 StoreQA
过程接收到 Optional
参数,它需要在使用之前验证它是否接收到有效的对象引用 - 否则会出现问题:
Sub StoreQA(Optional ByVal ShapeClicked As Shape)
If ShapeClicked Is Nothing Then
' argument was not provided *or 'Nothing' was supplied*
''Debug.Print ShapeClicked.Name '<~ error 91
Else
' ShapeClicked is holding a valid Shape object reference
Debug.Print ShapeClicked.Name
End If
End Sub
如果 Optional
参数被声明为 As Variant
(明确或不明确),那么您可以使用 IsMissing
函数来验证是否提供了可选参数 - 当 [=26] 时有用=] 也是一个有效的、有用的接收值(无论如何对于对象引用——对于普通值类型,它可以消除求助于 magic/hard-coded "invalid" 值的需要):
Sub StoreQA(Optional ByVal ShapeClicked As Variant)
If IsMissing(ShapeClicked) Then '<~ only valid with an Optional+Variant parameter
' argument was not provided
''Debug.Print ShapeClicked.Name '<~ error 91
ElseIf Not ShapedClicked Is Nothing Then
' ShapeClicked is holding a valid Shape object reference
Debug.Print ShapeClicked.Name
Else
' 'Nothing' was explicitly supplied
'...
End If
End Sub
Sub StoreQA(ShapeClicked As Shape)
MsgBox "yeee"
End Sub
Sub ThisIsToBeRun()
StoreQA
End Sub
我收到编译错误:参数不是可选的。
我尝试提及 StoreQA(ShapeClicked As Shape)
,但这也无济于事。
Argument not optional
您在这里定义了一个参数,名称为ShapeClicked
:
Sub StoreQA(ShapeClicked As Shape)
因为参数不是 Optional
,任何代码都必须 为这个参数提供参数 来调用这个 StoreQA
过程:
' gets the first Shape in Sheet1 (assumes it exists), then passes it to StoreQA:
StoreQA Sheet1.Shapes(1)
如果不需要该参数,请将其删除:
Sub StoreQA()
如果有的地方需要,有的地方不需要,可以考虑制作Optional
:
Sub StoreQA(Optional ByVal ShapeClicked As Shape)
那么您可以在不提供任何参数的情况下合法地调用该过程:
StoreQA '<~ compiles fine now
可选注意事项
如果 StoreQA
过程接收到 Optional
参数,它需要在使用之前验证它是否接收到有效的对象引用 - 否则会出现问题:
Sub StoreQA(Optional ByVal ShapeClicked As Shape)
If ShapeClicked Is Nothing Then
' argument was not provided *or 'Nothing' was supplied*
''Debug.Print ShapeClicked.Name '<~ error 91
Else
' ShapeClicked is holding a valid Shape object reference
Debug.Print ShapeClicked.Name
End If
End Sub
如果 Optional
参数被声明为 As Variant
(明确或不明确),那么您可以使用 IsMissing
函数来验证是否提供了可选参数 - 当 [=26] 时有用=] 也是一个有效的、有用的接收值(无论如何对于对象引用——对于普通值类型,它可以消除求助于 magic/hard-coded "invalid" 值的需要):
Sub StoreQA(Optional ByVal ShapeClicked As Variant)
If IsMissing(ShapeClicked) Then '<~ only valid with an Optional+Variant parameter
' argument was not provided
''Debug.Print ShapeClicked.Name '<~ error 91
ElseIf Not ShapedClicked Is Nothing Then
' ShapeClicked is holding a valid Shape object reference
Debug.Print ShapeClicked.Name
Else
' 'Nothing' was explicitly supplied
'...
End If
End Sub