Clear contents of an Excel worksheet with a command button

I've placed a command button on my Excel worksheet.

I want to be able to clear the contents (set them to zero) of cells C3 through C10, D3 through D10 and E3 through E10 by clicking on the button.

Each of the cells is formatted as "Accounting".

However, I want to give the user the chance to opt out of clearing the contents via a VBYesNo message.

Something like "Are you sure you want to clear the contents of this worksheet?"

If the user selects Yes, I not only want the cells' contents to be cleared but for the worksheet to be saved as well.

Thank you.
dbfromnewjerseyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
Try this.
29077831.xlsm
0
Martin LissOlder than dirtCommented:
I forgot about saving the sheet.
29077831a.xlsm
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NorieAnalyst Assistant Commented:
In the attached workbook I've created 2 buttons, one from the Forms toolbar and one from the ActiveX toolbar.

They both call the below code to clear the range on their respective sheets.
Sub ClearCells(ws As Worksheet)
Dim Ans As VbMsgBoxResult

    Ans = MsgBox("Are you sure you want to clear the contents" & vbCrLf & "of worksheet " & ws.Name & " ?", vbYesNo + vbQuestion, "Clear cells")
    
    If Ans = vbYes Then
        ws.Range("C3:E10").Value = 0
    End If
    
    ThisWorkbook.Save

End Sub

Open in new window

ClearCellsWithButton.xlsm
0
Martin LissOlder than dirtCommented:
Here is a project where a "msgbox" is created from a form that I wrote years ago. One problem you'll face is finding a vbExlamation icon or picture file.
message.zip
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.