Unprotect sheet error in VBA

willow1985

Well-known Member
Joined
Jul 24, 2019
Messages
886
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have an error with the below code and am not sure how to fix it. I want to unprotect the sheet (columns A:I, on Sheet "Batch Log"), paste the info in the first blank cell (that was copied from the New BL Data sheet) and re-protect columns A:I , however I get the error at the Selection.Locked = False portion of the code.

Error: 1004, unable to set the locked property of the range class

PS the locked sheet is not password protected. Thank you for your help with this.
Rich (BB code):
Sub NewDataBL()
'
' NewDataBL Macro


With ThisWorkbook.Sheets("New BL Data")
   If Application.CountIf(.Range("B2:I2"), "") > 0 Then
      MsgBox "Please Complete all Fields"
      Exit Sub
   End If
End With
    Range("A2:I2").Select
    Selection.Copy
Sheets("Batch Log").Select
Columns("A:I").Select
Range("BLTable[[#Headers],[TYPE]]").Activate
    Selection.Locked = False
    Selection.FormulaHidden = False
Range("BLTable").Cells(1, 1).End(xlDown).Offset(1).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
   Columns("A:I").Select
Range("BLTable[[#Headers],[TYPE]]").Activate
    Selection.Locked = True
    Selection.FormulaHidden = False
 Range("BLTable").Cells(1, 1).End(xlDown).Offset(1).Select
    Sheets("New BL Data").Select
    Range("A1").Select


'
End Sub
 
Last edited by a moderator:

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,)
you stated this was the code to protect and unprotect a worksheet, but how would you go about selecting the parameters of the locked sheet?

Example:
AllowSorting:=True, AllowFiltering:=True

Thank you

Carla
 
Upvote 0
This is the current code:

Worksheets("CAPA Log").Protect Password:="SADIE"

But I want to have the following parameters:

DrawingObjects:=True, Contents:=True, Scenarios:=True, AllowSorting:=True, AllowFiltering:=True

How do I combine this?

Thank you

Carla
 
Upvote 0
Simply record a macro of you protecting the sheet with the options you want & you will get the code you need.
 
Upvote 0
I did that but it no longer added my password.

This is the code I got even though I entered the password:

Sub Macro2()
'
' Macro2 Macro
'


'
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowSorting:=True, AllowFiltering:=True
End Sub
 
Last edited:
Upvote 0
Just add the
Code:
Password:="[COLOR=#333333]SADIE[/COLOR]"
to the end of the recorded code.
 
Upvote 0
I get compile error, expected expression on the "=" sign after password OR expected end of statement, depending where I have the Password line (same line or new line)

Worksheets("CAPA Log").Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowSorting:=True, AllowFiltering:=True Password:="SADIE"
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,622
Messages
6,120,580
Members
448,972
Latest member
Shantanu2024

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