VBA Protection

dfenton21

Board Regular
Joined
Jun 23, 2007
Messages
135
I have a macro that copies the contents of a cell, and pastes it into the the first blank cell of a range. Its important that the entire sheet is protected, but the macro won't allow the paste function because of the protection.

Is there a VBA code to unprotect the sheet, run the copy/paste macro, then protect the sheet again. THe problem is I would prefer the protection to use a password, as I don't want the user to simply unprotect the sheet from the menu bar.

Any assistence would be greatly appreciated.

Thanks.
 
For the sheets that you do not want protection, Go to the sheet, Press Ctrl + A, Press Ctrl + 1, Protection tab and uncheck the "Locked" option and click on OK.

You are done :biggrin:

Edit: The other way would be to change the code in order for the macro to run only on specific sheets but that would take some extra time and coding in the present code.

I would go for the uncheck "Locked" option.
 
Upvote 0

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
While using the code I noticed that some sheets have a different passwords, for me these errors are not interested.
How do I ignore this error and continue unprotecting the next sheet?
 
Upvote 0
Replacing the exsiting code with this code will stop displaying the "Incorrect Password" error message:

Code:
Option Explicit
 
Sub Protect_all()
     
    Dim wSheet          As Worksheet
    Dim Pwd             As String
     
    Pwd = InputBox("Enter your password to protect all worksheets", "Password Input")
    For Each wSheet In Worksheets
        wSheet.Protect Password:=Pwd
    Next wSheet
     
End Sub
 
Sub UnProtect_all()
     
    Dim wSheet          As Worksheet
    Dim Pwd             As String
     
    Pwd = InputBox("Enter your password to unprotect all worksheets", "Password Input")
    On Error Resume Next
    For Each wSheet In Worksheets
        wSheet.Unprotect Password:=Pwd
    Next wSheet
   'If Err <> 0 Then
   '   MsgBox "You have entered an incorect password. All worksheets could not " & _
   '   "be unprotected.", vbCritical, "Incorect Password"
   ' End If
    On Error GoTo 0
     
End Sub
 
Upvote 0
It certainly does the Job, but now I noticed that when setting the password again it crashes on, I guess password protected sheet.
So how do I check a sheet is already protected and then skip this sheet.

Sorry for the bother but I really do appreciate your help.
 
Upvote 0

Forum statistics

Threads
1,215,280
Messages
6,124,034
Members
449,139
Latest member
sramesh1024

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