By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,641 Members | 2,110 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,641 IT Pros & Developers. It's quick & easy.

Exit Sub Not Working

P: 2
I have a problem, the Exit Sub command is not working for some reason. If the user says NO to the MsgBox I want to stop the code, but the program just continues to execute.

I have use breakpoints and traced the code and found that the Exit Sub command is only working 50% of the time.

tempNumber = MsgBox("Try Again?", 292)
'If the user wants to try again
If tempNumber = 6 Then
' Continue
' The user wants to exit
Exit Sub

End If
Feb 28 '10 #1
Share this Question
Share on Google+
3 Replies

Expert 5K+
P: 8,634
Expand|Select|Wrap|Line Numbers
  1. Dim tempNumber As Integer
  3. tempNumber = MsgBox("Try Again?", vbYesNo + vbInformation + vbDefaultButton2)
  5. If tempNumber = vbNo Then Exit Sub
  7. 'Continue if User selects 'Yes'
Feb 28 '10 #2

P: 2
Thank you for your response i appreciate the help you're trying to give me. I rearranged my code as per your suggestion but I'm running into the same problem.

My code is setup so that if the user wants to retry I am calling my function again and starting from the beginning. So I keep on looping if the user makes a mistake and wants to re-enter information.

If the user wants to exit after making a mistake the FIRST time , the program ends properly. If the user chooses to try again then makes the mistake again and then clicks no, the program comes to the Exit Sub command again and just continues with the code as if it executed it.

Expand|Select|Wrap|Line Numbers
  1. Private Sub btnLedgerEnter_Click()
  3. Dim tempNumber As Integer
  5. tempNumber = MsgBox("Try Again?", vbYesNo + vbInformation + vbDefaultButton2)
  7. If tempNumber = vbNo Then Exit Sub
  9. else
  10.   btnLedgerEnter_Click
  11. end if
  12. ' MORE CODE
  13. end sub
Feb 28 '10 #3

Expert 2.5K+
P: 3,532
You can have a one-line If...Then statement

If tempNumber = vbNo Then Exit Sub

but you cannot use that If for the followup Else and End If. Try

Expand|Select|Wrap|Line Numbers
  1. If tempNumber = vbNo Then 
  2.   Exit Sub
  3. Else
  4.    btnLedgerEnter_Click
  5. End if
Welcome to Bytes!

Linq ;0)>
Feb 28 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.