Code mysteriously failing to initiate after selecting the "Cancel" option in a message box.

bellg

New Member
Joined
May 13, 2014
Messages
29
I have one subroutine that calls many other subroutines based on a number of buttons/conditions I've set up in my Excel worksheet. Below is a truncated example of this.

Code:
Public a as Integer
Public ans as Integer

Public Sub StartingMacro()

   If Application.Caller = "button1" Then Call Macro1
   If Application.Caller = "button2" Then Call Macro2
   If Application.Caller = "button2" Then Call Macro3
   If a = 1 Then Call Macro6
   If a = 2 Then Call Macro7
   If a <> 3 Then Call Macro8

End Sub


I have this line of code in many of my Macros that get called by "StartingMacro":

Code:
ans = MsgBox("Are you sure you want to do something?", vbYesNoCancel)
If ans <> vbYes Then Exit Sub

When the user selects "No" or "Cancel", assigning ans = vbNo or ans = vbCancel, it exits whatever Macro it's in and finishes through the "StartingMacro". I can step through my code and I can see it do this without a problem. However, in the instance where I select "Cancel", the buttons in my worksheet no longer call macros after the "StartingMacro" subroutine completes through to the end. It completely finishes, but it's like the code is in debug mode or something without showing it in the VB Editor window. I literally have to go into the VB Editor and hit the square "Reset" button if I ever select "Cancel" to a message box. Remember, this is after I step through the code until the "End Sub" line without issues. The most curious part is in the instance when I select "No" because this phenomenon doesn't happen at all. The code finishes through the "StartingMacro", and I can select other buttons in the worksheet having them call other Macros without a problem. The only difference is answering "No" or "Cancel".

Does anyone know why that happens?

NOTE: I know I can create a message box with only Yes/No, but I want all three options. I also know that if I change the "ans" variable to "ansOther" (or some other name) then the "Cancel" option works just fine. However, I use the "ans" variable everywhere in my code (it is lengthy) to simplify the number of variables I have to declare/use, so declaring a custom variable every time I use a message box sounds dreadful. I'm really looking for an answer to the difference between the user selecting "No" and "Cancel" in relation to my problem.

DOES ANYONE KNOW WHY THIS HAPPENS?
 
Last edited:
Anthony said:
You are using "Public" variable: keep in mind that they retain any set value until a new value is set, or the file is closed, or the square "Reset" is pressed.
bellg said:
I found the problem - thanks for the debugger idea. Public variables do not reset automatically when the code finishes execution, so the "ans" variable was not resetting to 0
Eh eh :biggrin::biggrin:...

Variable defined at "Module" level (be they Public or private) are designed with that behaviour. It is good practice keeping to a minimum this type of variable.
See the vba help online, "Validity and visibility of variable" and "Variable life time"

As Fluff already suggested, you can create a Sub that clears all the Public or anyway the variable defined on top of Modules; something like
Code:
Sub ClearAll()
Variable1 = Empty        
Variable2 = Empty
'etc
End Sub
This assume that Variable1, Variable2 etc are Public or defined on top of Modules
Then you will use
Code:
Call ClearAll

Bye
 
Upvote 0

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.

Forum statistics

Threads
1,215,758
Messages
6,126,706
Members
449,331
Latest member
smckenzie2016

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