如何检测何时可选的ByRef参数未被传递
我试图检测一个opational参数是否通过,但由于某些原因,所有常用函数(IsMissing()/ IsEmpty()/ IsNull())总是返回false。
这就是我想要的:
Public Sub SetValue(Key As String, Optional ByRef ws As Worksheet) If IsMissing(ws) Or IsEmpty(ws) Or IsNull(ws) Then ws = ThisWorkbook.Sheets(SheetName) End If
我也尝试将ws设置为Nothing或Null,但结果是一样的:
Public Sub SetValue(Key As String, Optional ByRef ws As Worksheet = Nothing) If IsMissing(ws) Or IsEmpty(ws) Or IsNull(ws) Then ws = ThisWorkbook.Sheets(SheetName) End If
任何想法为什么这可能会发生?
尝试is nothing
Private Sub CommandButton1_Click() Dim ws As Excel.Worksheet Set ws = ActiveWorkbook.Sheets("sheet1") 'Call the sub both ways. SetValue "a" SetValue "a", ws End sub Public Sub SetValue(Key As String, Optional ByRef ws As Worksheet) If ws Is Nothing Then 'We got a sheet MsgBox "We got no sheet" End If If Not ws Is Nothing Then 'We got a sheet MsgBox ws.name MsgBox "We got a sheet" End If End sub
build设是没有什么工作。 我在所有的参数中使用它。
Public Sub SetValue(ByRef Key As String, _ Optional ByRef ws As Worksheet = Nothing) 'Make sure we have the object If ws Is Nothing Then Set ws = ThisWorkbook.Sheets(SheetName) End If End Sub
希望这可以帮助。 🙂