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
563
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

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
.
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,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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