View Full Version : VBA Message Boxes


Pi-Sudoku
01-26-06, 02:21 PM
I need to run a VBA macro if and only if the user selects "yes" on another message box. Im i on the right lines


Sub macro1 ()
MsgBox "Run Macro2"
If MsgBox=1 Then
Macro2
End If
End Sub

Voodoo Child
01-26-06, 09:53 PM
I think you need to pass a constant to the MsgBox routine to tell it to show Yes and No buttons. eg. MsgBox("ne-nu", vbYesNoCancel(or some such integer constant))
I don't have anything VBA in front of me, but I think you do it thus:

Dim response As Int
response = MsgBox("do shit?", constant)
if response = 1 Then 'or whatever number the function returns
'do shit
EndIf

There are 5 or 6 args that do all the usual message box pullava- icons, defaults etc.

Course, I've only used VBA once in my life, so ...

Pi-Sudoku
01-30-06, 12:26 PM
I entered this code


response = MsgBox("would you like....", vbYesNo)
If response = 6 Then
Do Something
End If


but it does the task whether yes or no are pressed, any reasons why?

sas01
01-30-06, 02:49 PM
Because you have'nt defined wether the response has to be a yes...

Pete
01-30-06, 06:13 PM
I entered this code


response = MsgBox("would you like....", vbYesNo)
If response = 6 Then
Do Something
End If


but it does the task whether yes or no are pressed, any reasons why?
You can use vbYes instead of "6" if you like, but that should work.

Put a breakpoint on the "If response=6" line, and check the value of "response" for each button.