Cells getting locked when running VBA

andrewb90

Well-known Member
Joined
Dec 16, 2009
Messages
1,077
Hello all,

I have this code that I am using:
Code:
Sub completed()    ActiveSheet.Unprotect "majinbuu"
    
    Range("E11").Select
    ActiveCell.Value = Range("A9") & " " & Range("E43") & " " & Date & " " & Time
    Range("E13:E41").Select
    Selection.Locked = True
    Selection.FormulaHidden = False
    Range("E43").Select
    Selection.Locked = True
    Selection.FormulaHidden = False
    
    ActiveWorkbook.Save
    ActiveSheet.Protect "majinbuu"
    ActiveSheet.EnableSelection = xlUnlockedCells
End Sub

Now I have cells in F13:F41 that I am making sure are unchecked as "locked" however, anytime I run the code, these cells get locked in addition to the cells that I want to lock. Is there something I can do to fix this?

Thanks,
Andrew
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Maybe this...
Code:
Sub completed()
ActiveSheet.Unprotect "majinbuu"
Cells.Locked = False
Range("E11").Value = Range("A9") & " " & Range("E43") & " " & Date & " " & Time
With Range("E13:E41, E43")
    .Locked = True
    .FormulaHidden = False
End With
    ActiveSheet.Protect "majinbuu"
    ActiveSheet.EnableSelection = xlUnlockedCells
    ActiveWorkbook.Save
End Sub
 
Upvote 0
I received a runtime error 1004:
"Unable to set the locked property of the Range class"
 
Upvote 0
That usually means that the sheet is already protected so you can't change the cell format.
 
Upvote 0
The first line of the code is to unprotect the sheet. So that error can't be from that.
 
Upvote 0
So which line does throw the error ??
The code works fine for me !!
 
Upvote 0
As Teeroy mentioned
Is the sheet actually getting Unprotected ?
Is the password correct ?
otherwise is there merged cells involved?
 
Upvote 0
It was the merged cells, I had a few merged cells, and I didn't even think about how that wouldn't work. Thank you for the help!
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,212
Members
452,618
Latest member
Tam84

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