Adding warnings to a macro

Mr2017

Well-known Member
Joined
Nov 28, 2016
Messages
644
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi

I have a macro which clears data from cells A2 to H50,000.

However, I would like to add a warning message which asks the user if they're sure they want to proceed with the action.

If 'yes' then it proceeds, if 'No,' then nothing happens. My code is below with 'comment' additions of what I'd like to do.

Can someone please help? Thanks in advance.

Sub ClearDataFromCSVtab()
'Clears contents from columns A to H (below the titles) in the CSV data tab up to row 50,000
'MsgBox "Are you sure you want to clear the data in this tab?"


'If - answer is 'No' then nothing


else if
End If




ThisWorkbook.ActiveSheet.Range("A2:H50000").ClearContents
MsgBox "Data has been cleared"
End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Try this:
Code:
Sub ClearDataFromCSVtab()
'Clears contents from columns A to H (below the titles) in the CSV data tab up to row 50,000

Dim myMsg

myMsg = MsgBox("Are you sure you want to clear the data in this tab?", vbYesNo, "CONFIRMATION")

If myMsg = vbYes Then
    ThisWorkbook.ActiveSheet.Range("A2:H50000").ClearContents
    MsgBox "Data has been cleared"
Else
    MsgBox "Deletion Aborted"
End If

End Sub
 
Upvote 0
Thanks Joe4, I appreciate it.

Try this:
Code:
Sub ClearDataFromCSVtab()
'Clears contents from columns A to H (below the titles) in the CSV data tab up to row 50,000

Dim myMsg

myMsg = MsgBox("Are you sure you want to clear the data in this tab?", vbYesNo, "CONFIRMATION")

If myMsg = vbYes Then
    ThisWorkbook.ActiveSheet.Range("A2:H50000").ClearContents
    MsgBox "Data has been cleared"
Else
    MsgBox "Deletion Aborted"
End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,430
Messages
6,124,853
Members
449,194
Latest member
HellScout

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