Pass Msgbox answer to another sub

FryGirl

Well-known Member
Joined
Nov 11, 2008
Messages
1,364
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi All,

This works fine to retrieve the results from a MsgBox function and return the results to the sub that initiated the call, but would love to know how to pass the MsgBox answer from a normal sub back to the other sub.


Code:
Sub MsgboxYes()
    If MsgBox_Ans = vbYes Then MsgBox "You Seleted Yes"
End Sub

Code:
[LEFT][COLOR=#222222][FONT=Verdana]Function MsgBox_Ans()[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]    MsgBox_Ans = MsgBox("Yes or No", vbYesNo)[/FONT][/COLOR]
[COLOR=#222222][FONT=Verdana]End Function[/FONT][/COLOR][/LEFT]
<strike></strike>
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Code:
Public YourAnswer As String 'global variable


Sub CheckAnswer()
    If YourAnswer = 6 Then
        MsgBox "You Seleted Yes"
    Else
        MsgBox "You Seleted No"
    End If
End Sub


Sub AskMe()
    YourAnswer = MsgBox("Yes or No", vbYesNo)
End Sub
 
Upvote 0
I'm not quite seeing how this works. I would suspect I should start with the procedure Sub CheckAnswer(), but how does that call Sub AskMe(). Also, shouldn't the Public variable be an Interger?
 
Last edited:
Upvote 0
That's example how to keep something between subs.
Run AskMe (1st procedure), then your answer is 'write' into global variable, so when you run CheckAnswer (2nd procedure) you can grab YourAnswer var and check this out and deal with it.
Yes, change to integer.
 
Upvote 0
Okay sorry, my mistake, I see it now. Thank you for your help.
 
Upvote 0
How long will YourAnswer, the Public variable hold the ans? Is it until you close the workbook?
 
Upvote 0
If you want to keep Answer after closing and get it after opening, you have to write somewhere, I mean into any cell.
Then after opening you can read from this cell and for excel session variable will be available for all subs as YourAnswer.
I suggest something like: Workbook_Open event --> read cell value into var, WorkBook_BeforeClose event --> write var into cell.
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,173
Members
451,543
Latest member
cesymcox

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