Consulting

Results 1 to 7 of 7

Thread: Solved: How to answer the msg box automatically?

  1. #1
    VBAX Contributor
    Joined
    Nov 2009
    Posts
    114
    Location

    Solved: How to answer the msg box automatically?

    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?

  2. #2
    Quote Originally Posted by clif
    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?
    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

  3. #3
    VBAX Contributor CaptRon's Avatar
    Joined
    Mar 2007
    Location
    Austin, Texas
    Posts
    141
    Location
    Might try this:
    [vba]Application.OnKey "{DELETE}"[/vba]
    or
    [vba]Application.OnKey "{DEL}"[/vba]

    Ron
    Before you criticize someone, you should walk a mile in their shoes. That way, when you criticize them, you're a mile away and you have their shoes.

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Quote Originally Posted by clif
    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

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

    Paul

  5. #5
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    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.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  6. #6
    VBAX Contributor
    Joined
    Nov 2009
    Posts
    114
    Location
    Thanks all

  7. #7
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    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?
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •