PDA

View Full Version : Solved: How to answer the msg box automatically?



clif
02-26-2010, 06:46 PM
When I run the macro in excel, there is a message box come out.
There is two options "Delete" and "Cancel". I always need to press the "Delete" button. What is the vba code for pressing the "Delete" button?
:rotlaugh:

domfootwear
02-26-2010, 07:06 PM
When I run the macro in excel, there is a message box come out.
There is two options "Delete" and "Cancel". I always need to press the "Delete" button. What is the vba code for pressing the "Delete" button?
:rotlaugh:

If you designed userform for confirm delete, you need to set property for command Delete (Default=true)

If use Msgbox, try this code:


Sub AskAndDo()
If MsgBox("Are you sure ?", vbYesNo + vbQuestion) = vbNo Then
Exit Sub
Else
'Code goes here

End If
End Sub

CaptRon
02-26-2010, 07:08 PM
Might try this:
Application.OnKey "{DELETE}"
or
Application.OnKey "{DEL}"

Ron

Paul_Hossler
02-26-2010, 07:13 PM
When I run the macro in excel, there is a message box come out.
There is two options "Delete" and "Cancel". I always need to press the "Delete" button. What is the vba code for pressing the "Delete" button?


Well, if it's your macro and you always press the delete button, program it to to not have the Delete/Cancel dialog box at all.

Now ,,, guessing here ... if it's an Excel box, like when your macro deletes a worksheet and Excel asks "Are You Sure?", then you can control that by .DisplayAlerts as below


Application.DisplayAlerts = False
On Error Resume Next
Worksheets(ws).Delete
On Error GoTo 0
Application.DisplayAlerts = True


Paul

lucas
02-26-2010, 07:42 PM
clif, Paul is guessing because you didn't provide the information necessary to answer your question.

How about posting the code in question? That would help us in our efforts to help you and it would probably not have taken as many posts as it did so far because no one knows what you are looking at.

clif
02-26-2010, 07:50 PM
Thanks all

lucas
02-26-2010, 07:57 PM
clif, did Paul's answer solve your problem? If so, could you please mark your thread solved using the thread tools at the top of the page?