Password protecting macros

mvhh

New Member
Joined
Apr 24, 2011
Messages
14
I want to put a password on a macro as I keep accidentally pressing it and i can't get the information back that it erases. I have tried to follow the codes from other similar threads but they all come up with error messages. This is the code:

Sub ClearTable()
'
' ClearTable Macro
' Macro recorded 24/04/2011 by Any User
'
'
Range("B6:E15").Select
Selection.ClearContents
End Sub



HELP!!
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Perhaps like this

Code:
Sub ClearTable()
'
' ClearTable Macro
' Macro recorded 24/04/2011 by Any User
'
'
If MsgBox("Are you sure you want to clear B6:E15", vbQuestion + vbYesNo) = vbYes Then Range("B6:E15").ClearContents
End Sub
 
Upvote 0
thanks that worked but is there a way i can also put a password on it so that not anyone can press it?
 
Upvote 0
Try

Code:
Sub ClearTable()
'
' ClearTable Macro
' Macro recorded 24/04/2011 by Any User
'
'
If InputBox("Enter password") = "password" Then Range("B6:E15").ClearContents
End Sub
 
Upvote 0
Sub ClearTable()
'
' ClearTable Macro
' Macro recorded 25/04/2011 by Any User
'
'
Range("B6:D18").Select
Selection.ClearContents
Range("F7:F20").Select
Selection.ClearContents
End Sub

i changed the format of the table and tried to apply the password to this but everytime i do it it says 'can't execute in break mode'. help please??
 
Upvote 0
In the code window click the Reset button (just under Run) then try

Code:
Sub ClearTable()
'
' ClearTable Macro
' Macro recorded 25/04/2011 by Any User
'
'
If InputBox("Enter password") = "password" Then Range("B6:D18,F7:F20").ClearContents
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,616
Messages
6,179,908
Members
452,949
Latest member
beartooth91

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