卸载语句不会卸载UserForm

我相信这有一个简单的答案,但它让我难住。 以下代码是我在VBA中创build的用户表单的一部分。 表单询问用户在一个字段中的项目名称,以及该项目在另一个字段中的数量。 这个代码是用户在input信息后点击“okay”后运行的一部分。 下面的If-Else Statement显示检查项目(ItemName)是否出现在名为Inventory的工作表上。 如果没有出现,那么用户应该看到一条错误消息,代码应该停止运行。 但是,我的代码不停止运行。 在If-Else声明之后,似乎忽略了“卸载我”这一行并继续执行代码。 我在这里做错了什么?

Private Sub cmdOkay_Click() Dim Quantity As Double Dim ItemName as String Dim FoundRange As Range ItemName = Item_Name_Field Quantity = Quantity_Field Sheets("Inventory").Select Range("A1").Select Set FoundRange = Cells.Find(What:=ItemName, After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False) If FoundRange Is Nothing Then Dim AlertBox As Double AlertBox = MsgBox(ItemName & " was not found in Inventory. Check your spelling and try again.", vbExclamation, "Item Not Found") Unload Me Else '.... '(more code) End If '(more code) End Sub 

退出小组应该得到你想要的结果:

  Dim AlertBox As Double AlertBox = MsgBox(ItemName & " was not found in Inventory. Check your spelling and try again.", vbExclamation, "Item Not Found") Unload Me Exit Sub