Messagebox to run macro not working

Nikeyg

New Member
Joined
Jan 22, 2018
Messages
15
I'm trying to use an Ok/Cancel Message box to run a macro, however, it's just not working

If I use the below code regardless of if Ok or Cancel is pushed the macro will run
Code:
Sub Button()'
' Button Macro
'
Dim Answer As String


MsgBox "WARNING: The following process will clear all of today's submitted trades! Do you wish to proceed?", vbOKCancel + vbQuestion


Run "EODBACKUP"


End Sub

However, if I use the below regardless of if Ok or Cancel is pushed the macro doesn't run at all
Code:
Sub Button()'
' Button Macro
'
Dim Answer As String


MsgBox "WARNING: The following process will clear all of today's submitted trades! Do you wish to proceed?", vbOKCancel + vbQuestion


If ans = vbOK Then


Run "EODBACKUP"


End If


End Sub

Can someone please advise me what I'm doing wrong here?

Thanks in advance
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
You declared variable 'Answer' and later on you deal with 'ans'...

Try like this:


Code:
Sub Button() '
Dim answer As String
answer = MsgBox("WARNING: The following process will clear all of today's submitted trades! Do you wish to proceed?", vbOKCancel + vbQuestion)
If answer = vbOK Then
    MsgBox ("Macro will run.")
    'Call YOUR_MACRO
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,383
Members
448,955
Latest member
BatCoder

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