unprotect range vba

Kaps_mr2

Well-known Member
Joined
Jul 5, 2008
Messages
1,583
Office Version
  1. 365
Platform
  1. Windows
Dear All,

I am trying to unprotect certain parts of a workbook. I would have thought that the following would work but no luck :-

I get "object does not support this property or method":-

Rich (BB code):
Invoicing_Tool.Sheets("Job List").Protect


'Big jobs

For j = 5 To 19

If Len(Invoicing_Tool.Sheets("Job List").Range("B" & j).Value) = 0 Then
'unprotect ranges

Set range_to_unprotect = Invoicing_Tool.Sheets("Job List").Range("B" & j & ":F" & j)

range_to_unprotect.Unprotect

Set range_to_unprotect = Invoicing_Tool.Sheets("Job List").Range("H" & j)

range_to_unprotect.Unprotect

Set range_to_unprotect = Invoicing_Tool.Sheets("Job List").Range("j" & j * ":W" & j)

range_to_unprotect.Unprotect



End If


Next j
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Protection is applied to the SHEET, not a range within the Sheet..

So you have to unprotect the sheet.

Sheets("Job List").Unprotect

Hope that helps..
 
Upvote 0
I think you need to use UNLOCK/LOCK
You protect WorkSheets, You Lock and UnLock cells. Note the WorkSheet must be Unprotected to change the Locked Property of a cell

lenze
 
Upvote 0
I have tried the Locked cells method (I presume it can apply to a range) but I can still edit the cell contents - which I don't want. Here is my code - any suggestions :-

Code:
Public Sub Protect_Sheet()
Dim j As Long
Dim range_to_protect As Range


Invoicing_Tool.Sheets("Job List").Unprotect


'Big jobs


For j = 5 To 19

        If Len(Invoicing_Tool.Sheets("Job List").Range("B" & j).Value) > 0 Then
        'unprotect ranges
        
            Set range_to_protect = Invoicing_Tool.Sheets("Job List").Range("B" & j & ":F" & j)
            
            range_to_protect.Locked = True
            
            Set range_to_protect = Invoicing_Tool.Sheets("Job List").Range("H" & j)
            
            range_to_protect.Locked = True
            
            Set range_to_protect = Invoicing_Tool.Sheets("Job List").Range("j" & j & ":W" & j)
            
            range_to_protect.Locked = True
            
            
        
        End If
        
        
Next j

MsgBox "here"

End Sub

thanks

Kaps
 
Upvote 0
You need to re-protect the sheet (line in red)

Rich (BB code):
Public Sub Protect_Sheet()
Dim j As Long
Dim range_to_protect As Range


Invoicing_Tool.Sheets("Job List").Unprotect


'Big jobs


For j = 5 To 19

        If Len(Invoicing_Tool.Sheets("Job List").Range("B" & j).Value) > 0 Then
        'unprotect ranges
        
            Set range_to_protect = Invoicing_Tool.Sheets("Job List").Range("B" & j & ":F" & j)
            
            range_to_protect.Locked = True
            
            Set range_to_protect = Invoicing_Tool.Sheets("Job List").Range("H" & j)
            
            range_to_protect.Locked = True
            
            Set range_to_protect = Invoicing_Tool.Sheets("Job List").Range("j" & j & ":W" & j)
            
            range_to_protect.Locked = True
            
            
        
        End If
        
        
Next j

Invoicing_Tool.Sheets("Job List").Protect
MsgBox "here"

End Sub
 
Upvote 0
Peter,

Thanks - I tried this but now the whole sheet is protected rather certain ranges.

Kaps
 
Upvote 0
Try adding the line in red

Rich (BB code):
For j = 5 To 19

        If Len(Invoicing_Tool.Sheets("Job List").Range("B" & j).Value) > 0 Then
        'unprotect ranges
            Invoicing_Tool.Sheets("Job List").UsedRange.Locked = False
            
            Set range_to_protect = Invoicing_Tool.Sheets("Job List").Range("B" & j & ":F" & j)
 
Upvote 0
again I've tried that and it doesn't do what I expect. Certain parts are protected but not the bits I want.My code is now :-

thanks

Kaps

Code:
Public Sub Protect_Sheet()
Dim j As Long
Dim range_to_protect As Range


Invoicing_Tool.Sheets("Job List").Unprotect


'Big jobs


For j = 5 To 19

        If Len(Invoicing_Tool.Sheets("Job List").Range("B" & j).Value) > 0 Then
        'unprotect ranges
        
        Invoicing_Tool.Sheets("Job List").UsedRange.Locked = False

            Set range_to_protect = Invoicing_Tool.Sheets("Job List").Range("B" & j & ":F" & j)
            
            range_to_protect.Locked = True
            
            Set range_to_protect = Invoicing_Tool.Sheets("Job List").Range("H" & j)
            
            range_to_protect.Locked = True
            
            Set range_to_protect = Invoicing_Tool.Sheets("Job List").Range("j" & j & ":W" & j)
            
            range_to_protect.Locked = True
            
            
        
        End If
        
        
Next j


Invoicing_Tool.Sheets("Job List").Protect

Thanks

Kaps
 
Upvote 0
Maybe the unlocking needs to come out of the loop:

Rich (BB code):
Public Sub Protect_Sheet()
Dim j As Long
Dim range_to_protect As Range


Invoicing_Tool.Sheets("Job List").Unprotect

Invoicing_Tool.Sheets("Job List").UsedRange.Locked = False

'Big jobs


For j = 5 To 19

        If Len(Invoicing_Tool.Sheets("Job List").Range("B" & j).Value) > 0 Then
        'unprotect ranges
            
            Set range_to_protect = Invoicing_Tool.Sheets("Job List").Range("B" & j & ":F" & j)
            
            range_to_protect.Locked = True
            
            Set range_to_protect = Invoicing_Tool.Sheets("Job List").Range("H" & j)
            
            range_to_protect.Locked = True
            
            Set range_to_protect = Invoicing_Tool.Sheets("Job List").Range("j" & j & ":W" & j)
            
            range_to_protect.Locked = True
            
            
        
        End If
        
        
Next j

Invoicing_Tool.Sheets("Job List").Protect
MsgBox "here"

End Sub
 
Upvote 0

Forum statistics

Threads
1,216,119
Messages
6,128,946
Members
449,480
Latest member
yesitisasport

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