Code won't stop looping even when the cancel button is pressed within an input box

Pookiemeister

Well-known Member
Joined
Jan 6, 2012
Messages
626
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
When I click on the cancel button within the inputbox it gets stuck in the loop. How can I exit sub when cancel is clicked. Thank you.

Code:
Sub splitLot()    
    answ = ""


    ans = MsgBox("Is this a split lot?", vbYesNo + vbDefaultButton1, "Split Lot")
    
    If ans = vbNo Then
        frmPackageYield.Show vbModeless
    Else
        Call InptBx1
        If answ = "" Then
            Do
                MsgBox "Please enter a numerical value." & vbCrLf & "Number must be greater than one.", vbInformation + vbOKOnly, "No Value Entered"
                Call InptBx1
            Loop Until answ <> ""
        End If
        
        frmPackageYield.Show vbModeless
    End If
End Sub
Sub InptBx1()
    answ = InputBox("How many times does this lot split?", "Split Lot")
End Sub
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
.
Untested here but should work :

Code:
 If ans = vbNo Then
        frmPackageYield.Show vbModeless
Exit Sub
    Else
        Call InptBx1
        If answ = "" Then
 
Upvote 0
.
Similar code :

Code:
Option Explicit


Sub Test3()
Dim answer As Variant
  answer = MsgBox("Yes = run Test1; No = run Test2", vbYesNo + vbQuestion, "Choose macro to run")
  If answer = vbYes Then
      Call Test1
   Else
     'Call Test2
     Exit Sub
End If
End Sub


Sub Test1()
   Range("A1").Value = "A"
End Sub


Sub Test2()
   Range("A2").Value = "B"
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,632
Messages
6,173,472
Members
452,516
Latest member
archcalx

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top