Macro with Yes/No Calling Another Yes/No Macro - How to Disable the 2nd Yes/No

Snake Eyes

Board Regular
Joined
Dec 14, 2010
Messages
105
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have two macros that each have a Yes/No text box.
I would like to call Macro 2 from within Macro 1 however, in doing so, eliminate the Yes/No portion of Macro 2.
The catch is that Macro 2 needs to retain it's own Yes/No VB code since it can be run on it's own as well.

As you can see in Macro 1, I will ultimately be using the solution to actually call 3 macros from Macro 1.

Macro 1:
VBA Code:
Sub ClearALLSection_11()

'
' ClearALLSection_11

  Application.GoTo Reference:=("Section11_Position, Section11_Options, Section11_OptionsNotes")
  
       
' YesNoMessageBox()
Dim Answer As String
Dim MyNote As String

'Place your text here
    MyNote = "Do you really want to Clear the ALL Entries in Section 11?"

'Display MessageBox
    Answer = MsgBox(MyNote, vbQuestion + vbYesNo, "Clear All Entries in Section 11?")

    If Answer = vbNo Then
        Application.GoTo Reference:="Section11_PositionHome"
    End If
    
    If Answer = vbYes Then
        Selection.ClearContents


Macro 2
VBA Code:
Sub ClearPosition_11()

'
' ClearPosition_11 Macro

    Application.GoTo Reference:="Section11_Position"

   
' YesNoMessageBox()
Dim Answer As String
Dim MyNote As String

'Place your text here
    MyNote = "Do you really want to Clear the POSITION Entries in Section 11?"

'Display MessageBox
    Answer = MsgBox(MyNote, vbQuestion + vbYesNo, "Clear POSITION Entries in Section 11?")

    If Answer = vbNo Then
        Application.GoTo Reference:="Section11_PositionHome"
    End If
    
    If Answer = vbYes Then
        Selection.ClearContents
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
One way: add a parameter to the called sub and from the first sub, pass a value (e.g. Boolean False or True) to it. In the called sub check for the value of the passed parameter. If it's False, don't run the part you don't want to run. If True, then run (all?) of it.

2nd sub:
VBA Code:
Sub ClearPosition_11(bolMsg As Boolean)
If bolMsg = True Then msgbox "your message" <<if False, no message and code inside the If block doesn't execute


1st sub
VBA Code:
If [I]some condition[/I] Then 
   ClearPosition_11, True << pass True to 2nd sub
Else
   ClearPosition_11, False << pass False to 2nd sub
End If
You could also use module level variable so that each procedure has access to it. That might be better if you have several interdependent subs.
 
Upvote 0
Thanks for the code Micron.
I'm not the best with the how to insert your code properly into the two macros.
1) Not sure if the code after the "<<" is you giving me information or if it's part of the code to run.
2) I don't see where the "call" for the 2nd macro is in your code. Should I add it or are the lines...
VBA Code:
ClearPosition_11, True << pass True to 2nd sub
calling of the macro?

I've tried various entries of your code where I believe it goes but I'm just not getting it.
I appreciate the assistance.
 
Upvote 0
Yes those are notes and instructions which I should have commented out. I intended to just use general text but switched to code tags to maintain indentation & forgot. I didn't suggest how to call anything because there's no call in either of your subs so I have no idea where any calls would go. BTW, message box returns a number so it's better to Dim your result variable as an integer and use the return number values or you can use vb constants (e.g. vbYes) instead.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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