populate message box from the second running the macro

Mussa

Active Member
Joined
Jul 12, 2021
Messages
264
Office Version
  1. 2019
  2. 2010
Hi
I have procedure to populate message box before the run my macro
VBA Code:
ans = MsgBox("are you sure you want to continue ?", vbYesNo)
If ans = vbNo Then Exit Sub
'my macro
'
'
what I want the message box should show from the second running when click the button for my macro , not from the first time click the button , is it possible , guys?
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Hi @Mussa
Stupid question: Do you mean "second" (like first, second, third) or "second" (like hour, minute, seond)? :)
 
Upvote 0
try something like this:
VBA Code:
Public firstrun As Boolean
Sub test()
MsgBox firstrun
If firstrun Then

ans = MsgBox("are you sure you want to continue ?", vbYesNo)
If ans = vbNo Then Exit Sub
'my macro
End If
firstrun = True
End Sub
 
Upvote 0
Stupid question:
also your guessing is too stupid !
my post is really clear
(like hour, minute, seond)?
who say it?!
I clarified when click the button .
you don't click the button more than one time for any macro before?!
 
Last edited:
Upvote 0
Hi offthelip,
just show message box is empty when run the code every time.
 
Upvote 0
@offthelip
when click the button from first time should run my macro without show any messagebox
if I run again then will show the messagebox as in OP and select yes will run my macro again , select no the doesn't run the macro .
 
Upvote 0
I put a msgbox in the code to show you what was happening just delete it like this:
VBA Code:
Public firstrun As Boolean
Sub test()

If firstrun Then

ans = MsgBox("are you sure you want to continue ?", vbYesNo)
If ans = vbNo Then Exit Sub
End If
firstrun = True
'my macro
End Sub
Where have you put the code ? you need to have this code at the top of standard module so that the public declaration is the first thing in the module,
I have realised you need to move where "your macror" is too
 
Upvote 0
Where have you put the code ? you need to have this code at the top of standard module so that the public declaration is the first thing in the module,
yes this is my bad, sorry!
but still does not do as I want
ok look at this
VBA Code:
Public firstrun As Boolean

Sub test()

If firstrun Then

ans = MsgBox("are you sure you want to continue ?", vbYesNo)
If ans = vbNo Then Exit Sub
Range("a1").Value = "aA"
End If
firstrun = True

End Sub
my goal is when run the macro from the first time then will fill in A1 cell directly aA and when run again will show messgagebox if I click yes will fill in A1=aA and if I click no will not fill in A1 cell
based on your code doesn't fill in A1 and doesn't show message when run the first time .
thanks again
 
Upvote 0
nearly there try this:
VBA Code:
Sub test2()

If firstrun Then

ans = MsgBox("are you sure you want to continue ?", vbYesNo)
If ans = vbNo Then Exit Sub
End If
Range("a1").Value = "aA"  ' i moved this
firstrun = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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