Warning Macro

M

MCheru

I want to modify this macro so that before it runs, a message box will pop up
and ask are you sure you want to delete? and then have two options yes and
no. If the operator clicks yes the contents will delete. If the operator
clicks no the contents will not be deleted. I know how to get a message box
to come up MsgBox "your message here", but I am not sure how to give it
options. Here is the macro I have now.

Sub ClearContentsMacro Macro ()
Application.Goto Reference:="R7C2"
Range("B7:H116").Select
Selection.ClearContents
Selection.ClearContents
Range("B2:D2").Select
Selection.ClearContents
Range("B3:D3").Select
Selection.ClearContents
Range("B7").Select
End Sub
 
B

Barb Reinhardt

You can do something like this

Dim Verify as VBMsgBoxResult

Verify = Msgbox("Do you want to delete?",vbyesno)

if vbyes then
'Delete whatever you want to delete
end if
 
B

Barb Reinhardt

Oops, that was a typo. Thanks for catching it.

Chip Pearson said:
will always be true. Use

if Verify = vbyes then

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)
 
M

MCheru

Thank you. You're code is great. I am having one problem. Perhaps I made a
mistake. Here is the code I am using.

Sub ClearContentsMacro()

Dim Verify As VbMsgBoxResult

Verify = MsgBox("Do you want to delete?", vbYesNo)

If Verify = vbYes Then

End If
Application.Goto Reference:="R7C2"
Range("B7:H116").Select
Selection.ClearContents
Selection.ClearContents
Range("B2:D2").Select
Selection.ClearContents
Range("B3:D3").Select
Selection.ClearContents
Range("B7").Select

End Sub


.......The challenge I am having is that even when I click No all the cells
are still getting deleted.
 
M

MCheru

Thank you for helping out!

Chip Pearson said:
will always be true. Use

if Verify = vbyes then

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)
 
M

MCheru

Thank you! You're code is great. I am having one problem. Perhaps I made a
mistake. Here is the code I am using.

Sub ClearContentsMacro()

Dim Verify As VbMsgBoxResult

Verify = MsgBox("Do you want to delete?", vbYesNo)

If Verify = vbYes Then

End If
Application.Goto Reference:="R7C2"
Range("B7:H116").Select
Selection.ClearContents
Selection.ClearContents
Range("B2:D2").Select
Selection.ClearContents
Range("B3:D3").Select
Selection.ClearContents
Range("B7").Select

End Sub


.......The challenge I am having is that even when I click No all the cells
are still getting deleted.
 
D

Dave Peterson

Sub ClearContentsMacro()

Dim Verify As VbMsgBoxResult

Verify = MsgBox("Do you want to delete?", vbYesNo)

If Verify = vbYes Then
Application.Goto Reference:="R7C2"
Range("B7:H116").Select
Selection.ClearContents
Selection.ClearContents
Range("B2:D2").Select
Selection.ClearContents
Range("B3:D3").Select
Selection.ClearContents
Range("B7").Select
End If

End Sub

You could avoid the .select's with something like:

Sub ClearContentsMacro2()

Dim Verify As Long 'VbMsgBoxResult doesn't work in earlier versions

Verify = MsgBox("Do you want to delete?", vbYesNo)

If Verify = vbYes Then
Range("B7:H116").ClearContents
Range("B2:D2").ClearContents
Range("B3:D3").ClearContents
'Range("B7").Select 'did you really want to select B7
End If

End Sub

And you could even boil those 3 range().clearcontents to just one:

If Verify = vbYes Then
Range("B7:H116,B2:d3").ClearContents
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top