+ Reply to Thread
Results 1 to 8 of 8

Clear button, add warning dialogue box that asks: Are you sure you want to clear?

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-19-2012
    Location
    Croatia
    MS-Off Ver
    Excel 2007
    Posts
    200

    Clear button, add warning dialogue box that asks: Are you sure you want to clear?

    Hello!

    How do I add a feature to my function button that clears away data to submit a warning to the user before it clears any data?

    It can say something like:
    "Are you sure you want to clear all Inventory data?"
    With an option, "Yes" and "Cancel"

    Here is the current code for the "Clear" button:

    Sub Clear()
    '
    ' Clear Macro
    '
    
    '
        Range("C7:E91").Select
        Range("C7").Activate
        Selection.ClearContents
        ActiveCell.Select
    End Sub
    Thank you!

  2. #2
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Clear button, add warning dialogue box that asks: Are you sure you want to clear?

    For your original code.

    Sub Clear()
    
    Dim c02
    '
    ' Clear Macro
    '
    
    '
        c02 = MsgBox("Are you sure you want to clear?", vbYesNo, "Clear")
           If c02 = vbYes Then
              Range("C7:E91").ClearContents
           End If
    End Sub
    Last edited by JapanDave; 05-21-2012 at 10:55 AM. Reason: Left out code
    Be fore warned, I regularly post drunk. So don't take offence (too much) to what I say.
    I am the real 'Napster'
    The Grid. A digital frontier. I tried to picture clusters of information as they moved through the computer. What did they look like? Ships? motorcycles? Were the circuits like freeways? I kept dreaming of a world I thought I'd never see. And then, one day...

    If you receive help please give thanks. Click the * in the bottom left hand corner.

    snb's VBA Help Files

  3. #3
    Forum Contributor
    Join Date
    05-19-2012
    Location
    Croatia
    MS-Off Ver
    Excel 2007
    Posts
    200

    Re: Clear button, add warning dialogue box that asks: Are you sure you want to clear?

    Thanks.... but when I click "Cancel"
    It still clears away the data.

  4. #4
    Valued Forum Contributor AlvaroSiza's Avatar
    Join Date
    09-19-2007
    Location
    Staffordshire
    MS-Off Ver
    2007
    Posts
    591

    Re: Clear button, add warning dialogue box that asks: Are you sure you want to clear?

    Dim vbResponse As VbMsgBoxResult
    
    vbRepsonse = MsgBox("Are you sure that you would like to clear the contents of the range?", vbYesNo, "Clear?")
    
       If vbResponse = vbYes Then
          Range("C7:E91").ClearContents
       Else
          Exit Sub
       End If
    Perhaps it was the Noid who should have avoided me...
    If you are satisfied with my solution click the small star icon on the left. Thanks
    1. Make a copy of your workbook and run the following code on your copy (just in case)
    2. With excel open, press ALT+F11 to open the Visual Basic Editor (VBE). From the "Insert" menu, select "Module".
    3. Paste the code from above into the empty white space. Close the VBE.
    4. From the developer tab, choose "Macros", select the Sub Name, and click "Run".

  5. #5
    Forum Contributor
    Join Date
    05-19-2012
    Location
    Croatia
    MS-Off Ver
    Excel 2007
    Posts
    200

    Re: Clear button, add warning dialogue box that asks: Are you sure you want to clear?

    I tried this code, and I'm not sure what I'm doing... it seems not to be the entire code, so I try to integrate it with mine, but I don't know how. Could you please give me the entire code including the important parts of the original code... so that all I need to do is replace my existing code with a proper and complete code.

    This is what I have:
    Sub Clear()
    Dim vbResponse As VbMsgBoxResult
    
    vbRepsonse = MsgBox("Are you sure that you would like to clear the contents of the range?", vbYesNo, "Clear?")
    
       If vbResponse = vbYes Then
          Range("C7:E91").ClearContents
       Else
          Exit Sub
       End If
    
    End Sub
    All it does is display the message box. But regardless of my response to the box Yes/No... it does nothing else. But it needs to clear the field if I answer "Yes." And leave the field alone when I answer "No." It doesn't do that... please help me get the final code right....
    Last edited by nenadmail; 05-21-2012 at 01:42 PM.

  6. #6
    Valued Forum Contributor AlvaroSiza's Avatar
    Join Date
    09-19-2007
    Location
    Staffordshire
    MS-Off Ver
    2007
    Posts
    591

    Re: Clear button, add warning dialogue box that asks: Are you sure you want to clear?

    The vbRepsonse (right before = MsgBox) is misspelled. Fix the spelling to vbResponse (sorry about that) and it will work.

  7. #7
    Forum Contributor
    Join Date
    05-19-2012
    Location
    Croatia
    MS-Off Ver
    Excel 2007
    Posts
    200

    Re: Clear button, add warning dialogue box that asks: Are you sure you want to clear?

    Thanks! Yes now it works.... and here is the correct code for others:

    Sub Clear()
    Dim vbResponse As VbMsgBoxResult
    
    vbResponse = MsgBox("Are you sure that you would like to clear the contents of the range?", vbYesNo, "Clear?")
    
       If vbResponse = vbYes Then
          Range("C7:E91").ClearContents
       Else
          Exit Sub
       End If
    
    End Sub
    It is designed to clear the cells ("C7:E91"), but of course anyone can edit this to whatever they want cleared.

  8. #8
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Clear button, add warning dialogue box that asks: Are you sure you want to clear?

    I hope you did not use the first code I posted? I was lazy and left out an import line of code, plus I just realized I had a typo(it is too late here) Try mine or AlvaroSiza's code they will both do the job.
    Last edited by JapanDave; 05-21-2012 at 10:56 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1