![](/img/trans.png)
[英]Calling VBA subroutine does not work when it is called from inside of another subroutine
[英]Exit VBA Main Subroutine from Called Subroutine
我有一個子“ main”,它調用子“ prepare”以及其他子例程。 我在“准備”中有一個if語句,如果滿足條件,該語句將退出子程序。 但是,它僅退出該特定子例程,然后繼續執行“ main”中的所有其他子例程。
If oAltIDLocationDictionary.Exists(sAltID) Then
MsgBox "It appears that there are two duplicate ID's in your alternate ID list. Duplicate ID's cannot be processed, please consolidate the location information into a single ID or remove the duplicate ID from the Alt-ID list."
Exit Sub
End If
有沒有辦法從其正在調用的“准備”子句中退出“主”子句,以便在“准備”子句中滿足條件時,“主”子句停止並且不再執行任何代碼?
您可以將這些子項轉換為函數,如果這些函數返回某個值,則主子項將退出。
Sub Main()
Dim bTest As Boolean
' blah blah
bTest = Prepare
If bTest Then Exit Sub
' blah blah
End Sub
Function Prepare() As Boolean
Prepare = False
If oAltIDLocationDictionary.Exists(sAltID) Then
MsgBox "It appears that there are two duplicate ID's in your alternate ID list."
Prepare = True
Exit Function
End If
End Function
若要立即停止執行宏而不返回調用過程,可以使用End
語句。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.