Pop-up Yes/No message within VB code

kwoltman

New Member
Joined
Jun 29, 2012
Messages
25
Please note that I don't understand Modules, Sub/End Sub, etc so I'll need the exact/complete code if at all possible.

I have 2 tabs, tab 1 is called Instructions and tab 2 is called CCAN Data.
I recorded a macro (and cleaned it up just a bit based on what vb code I do know) and here's my code:
Sub ClearCCAN()
'
' ClearCCAN Macro
'
' Keyboard Shortcut: Ctrl+q
'
Sheets("CCAN Data").Select
Rows("2:1048576").Select
Selection.Delete Shift:=xlUp
Range("A2").Select
Sheets("Instructions").Select
Range("A6").Select
End Sub
I added a button on tab "Instructions" and I assigned my Macro to this button. When the user clicks this button, all data on rows 2-1048576 are deleted on the tab called "CCAN Data" and the user is returned to cell A6 on the Instructions tab.

What I'd like to add is a pop-up message that says:
Are you sure you want to delete all data on the CCAN Data tab? Yes / No

And the code only executes if the user clicks Yes, and nothing happens if the user clicks No.

Again, I really don't know VB Code (I'm just dangerous enough recording macros if you know what I mean). Can you please provide the complete set of code including what I already have shown above?

Many thanks in advance!!!
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
try this
Code:
Sub ClearCCAN()
'
' ClearCCAN Macro
'
' Keyboard Shortcut: Ctrl+q
'
X = MsgBox("Are you sure you want to delete?", vbYesNo, "YES/NO?")
If X = vbNo Then
Exit Sub
Else
End If
Sheets("CCAN Data").Select
Rows("2:1048576").Select
Selection.Delete Shift:=xlUp
Range("A2").Select
Sheets("Instructions").Select
Range("A6").Select
End Sub
 
Upvote 0
Welcome to the Board!

Note that you can reduce that to one line of code by not selecting:

Sheets("CCAN Data").Rows("2:1048576").Delete Shift:=xlUp

And this one will work in any version of Excel:

Sheets("CCAN Data").Rows("2:" & Cells(Rows.Count, "A").Row).Delete Shift:=xlUp

HTH,
 
Last edited:
Upvote 0

Forum statistics

Threads
1,213,527
Messages
6,114,144
Members
448,552
Latest member
WORKINGWITHNOLEADER

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