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.
 

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).
Before the Copy/Paste thing put:

ThisWorkbook.Unprotect ("Password")

After copy/paste thing runs put:
ThisWorkbook.Protect ("Password")

replace Password with your password..but make sure to use ""
 
Upvote 0
You can do that in code like this:

Code:
Sheets("Sheet1").Unprotect Password:="abc"
'
'do your stuff
'
Sheets("Sheet1").Protect Password:="abc"
 
Upvote 0
Presuming that you are just getting the value, you can also do this w/o ever unprotecting the sheet, by setting the UserInterfaceOnly arg to true - like:

Code:
    With ThisWorkbook.Worksheets("MySheet")
        .Protect "MyPassword", , , , True
        .Range("A3").Value = Worksheets("OtherSheet").Range("A5").Value
    End With

Mark
 
Upvote 0
I read the reply of Vog and this would be a base for what I need
which is 1000 Excelbooks that contain multiple protected sheets and now I need to:
a) Unprotect all sheets that are protected
b) Do my stuff
c) Return sheets that where protected before to protected state

any suggestions

Thanks in advance
David Sanders, Amsterdam
 
Upvote 0
hi again,

I am providing you with the code. The macro below will help you to Protect / UnProtect all the worksheets in one go in your workbook. Please copy and paste all of the code in one standard module:

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
This is real great and so fast!

The only "but" in this code is that it protects "all" the sheets even the sheets that where before unprotected.
 
Upvote 0

Forum statistics

Threads
1,214,534
Messages
6,120,086
Members
448,944
Latest member
sharmarick

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