自动检测带参数调用 VS 单独调用子例程?
Auto-detection of being called with an argument VS separately called for a subroutine?
我有三个子程序,有时 运行 连续,有时分开,但会使用相同的参数 ("TestNumber")。我的原始代码如下所示:
Sub Main1()
Dim TestNumber As String
TestNumber = InputBox("Please enter TestNumber")
' Some codes running on TestNumber
' Prompt users whether to continue to Main2
Dim Continue As String
Continue = MsgBox("Continue to Main2?", vbYesNo)
If Continue = vbYes Then
Call Main2
Else
End If
End Sub
Sub Main2() ' Similar to Main1, just that it will prompt if user wants to continue to Main3
Dim TestNumber As String
TestNumber = InputBox("Please enter TestNumber")
' Some codes running on TestNumber
Dim Continue As String
Continue = MsgBox("Continue to Main3?"), vbYesNo)
If Continue = vbYes Then
Call Main3
Else
End If
End Sub
Sub Main3() ' The last call of the series
Dim TestNumber As String
TestNumber = InputBox("Please enter TestNumber")
'Some codes running on TestNumber
End Sub
然后,我认为每次调用都重新输入 TestNumber 有点麻烦和冒险,所以我将代码更改为:
Sub Main1()
Dim TestNumber As String
TestNumber = InputBox("Please enter TestNumber")
' Some codes running on TestNumber
' Prompt users whether to continue to Main2
Dim Continue As String
Continue = MsgBox("Continue to Main2?", vbYesNo)
If Continue = vbYes Then
Call Main2(TestNumber)
Else
End If
End Sub
Sub Main2(TestNumber As String) ' Similar to Main1, just that it will prompt if user wants to continue to Main3
If TestNumber = "" Then
TestNumber = InputBox("Please enter TestNumber")
Else
End If
' Some codes running on TestNumber
Dim Continue As String
Continue = MsgBox("Continue to Main3?"), vbYesNo)
If Continue = vbYes Then
Call Main3(TestNumber)
Else
End If
End Sub
Sub Main3(TestNumber As String) ' The last call of the series
If TestNumber = "" Then
TestNumber = InputBox("Please enter TestNumber")
Else
End If
' Some codes running on TestNumber
End Sub
如果我从 Main1 开始一直工作到 Main3,这就很好用了。
但是,如果我从 Main2 或 Main3 开始,则脚本 运行s 都没有。相反,系统会提示 运行.
哪个子程序
有没有办法让子例程自动检测是带参数调用还是单独调用?
您可以将参数标记为可选并提供默认值,如下所示:
Sub Main2(Optional TestNumber As String = "")
或者您可以将参数更改为变体,将其标记为可选,然后测试参数是否使用 IsMissing 提供,如下所示:
Sub Main2(Optional TestNumber As Variant)
If IsMissing(TestNumber) Then
' do stuff
Else
' do other stuff
我有三个子程序,有时 运行 连续,有时分开,但会使用相同的参数 ("TestNumber")。我的原始代码如下所示:
Sub Main1()
Dim TestNumber As String
TestNumber = InputBox("Please enter TestNumber")
' Some codes running on TestNumber
' Prompt users whether to continue to Main2
Dim Continue As String
Continue = MsgBox("Continue to Main2?", vbYesNo)
If Continue = vbYes Then
Call Main2
Else
End If
End Sub
Sub Main2() ' Similar to Main1, just that it will prompt if user wants to continue to Main3
Dim TestNumber As String
TestNumber = InputBox("Please enter TestNumber")
' Some codes running on TestNumber
Dim Continue As String
Continue = MsgBox("Continue to Main3?"), vbYesNo)
If Continue = vbYes Then
Call Main3
Else
End If
End Sub
Sub Main3() ' The last call of the series
Dim TestNumber As String
TestNumber = InputBox("Please enter TestNumber")
'Some codes running on TestNumber
End Sub
然后,我认为每次调用都重新输入 TestNumber 有点麻烦和冒险,所以我将代码更改为:
Sub Main1()
Dim TestNumber As String
TestNumber = InputBox("Please enter TestNumber")
' Some codes running on TestNumber
' Prompt users whether to continue to Main2
Dim Continue As String
Continue = MsgBox("Continue to Main2?", vbYesNo)
If Continue = vbYes Then
Call Main2(TestNumber)
Else
End If
End Sub
Sub Main2(TestNumber As String) ' Similar to Main1, just that it will prompt if user wants to continue to Main3
If TestNumber = "" Then
TestNumber = InputBox("Please enter TestNumber")
Else
End If
' Some codes running on TestNumber
Dim Continue As String
Continue = MsgBox("Continue to Main3?"), vbYesNo)
If Continue = vbYes Then
Call Main3(TestNumber)
Else
End If
End Sub
Sub Main3(TestNumber As String) ' The last call of the series
If TestNumber = "" Then
TestNumber = InputBox("Please enter TestNumber")
Else
End If
' Some codes running on TestNumber
End Sub
如果我从 Main1 开始一直工作到 Main3,这就很好用了。 但是,如果我从 Main2 或 Main3 开始,则脚本 运行s 都没有。相反,系统会提示 运行.
哪个子程序有没有办法让子例程自动检测是带参数调用还是单独调用?
您可以将参数标记为可选并提供默认值,如下所示:
Sub Main2(Optional TestNumber As String = "")
或者您可以将参数更改为变体,将其标记为可选,然后测试参数是否使用 IsMissing 提供,如下所示:
Sub Main2(Optional TestNumber As Variant)
If IsMissing(TestNumber) Then
' do stuff
Else
' do other stuff