VBOKCancel issues

sassy

New Member
Joined
Feb 23, 2009
Messages
38
Hi, I need a little help from the experts!!! I have the following code and the cancel button is not working. It still runs the rest of the SUB. Is there a way, without using an IF statement to make the Cancel button on the message box work? Also, if i have to use an IF statement, do I have to change the DIM statement?

thanks!!!

Sub Clear_Sheet()
'This will clear the data entry sheet only. only has cell that are completed as of 1/17/2011
Dim Response As VbMsgBoxResult
'
Response = MsgBox("Are you sure you want to clear the sheet?", vbOKCancel, "Clear Sheet?")
Worksheets("Data Entry").Select
ActiveSheet.Unprotect
'
Range("I92,D2:E2,D4:E4,D6:E6,D8:E8,D10:E10,H18,B20:H20,H24,B26:H26").Select
Range("B26").Activate
Selection.ClearContents
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
You probably need to just assess Response

something like

Code:
If Response = vbCancel Then Exit Sub

Should stop the rest of the code being executed.
 
Upvote 0
Response = MsgBox("Are you sure you want to clear the sheet?", vbOKCancel, "Clear Sheet?")
All this command does is get the user's response and store it in Response. As Dave says, you need to test what the user's response actually is otherwise VBA can't know what to do. I mean, it doesn't know whether you asked "Are you sure you want to clear the sheet?" or "Shall I leave the contents of the sheet intact?".

So no, you do need to use an IF to test the response and take the appropriate action, exactly as Dave has specified.
 
Upvote 0
insert breakpoint on code line after response - when code pauses - put cursor on responce - you can see value - will be an integer. If you dont know how to add breakpoint then insert

STOP

after response line - now place cursor on response - this is a bad habbit
 
Upvote 0

Forum statistics

Threads
1,224,594
Messages
6,179,794
Members
452,943
Latest member
Newbie4296

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top