VBA Lock cells

Pinaceous

Well-known Member
Joined
Jun 11, 2014
Messages
1,113
Office Version
  1. 365
Platform
  1. Windows
Good Day Everyone,

I'm working with a sheet that has all of its sheet cells locked with a password in respect to its workbook and protect sheet functions:

VBA Code:
Sub LockWorkbook()

    ActiveWorkbook.Protect "Password", Structure:=True, Windows:=True
   
End Sub

VBA Code:
Sub ProtectSheets()

Dim ws As Worksheet
Dim pwd As String

pwd = "Password" 'Put your password here
For Each ws In Worksheets
    ws.Protect Password:=pwd
Next ws


End Sub


I'd like all of the cells upon to remain locked except "B11:AF180, AH11:AH180" upon that sheet with new sub.

Does anyone have any advice as I am stumped, thanks!

Respectfully,
pinaceous
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
try this


VBA Code:
Sub ProtectSheets()


Dim ws As Worksheet
Dim pwd As String


    pwd = "Password" 'Put your password here
   
For Each ws In Worksheets
On Error Resume Next

    ws.Protection.AllowEditRanges.Add Title:="Range1", Range:=Range( _
        "B11:AF180,AH11:AH180")
       
    ws.Protect pwd, DrawingObjects:=True, Contents:=True, Scenarios:=True
   
   
Next ws


End Sub
 
Upvote 0
try this


VBA Code:
Sub ProtectSheets()


Dim ws As Worksheet
Dim pwd As String


    pwd = "Password" 'Put your password here
  
For Each ws In Worksheets
On Error Resume Next

    ws.Protection.AllowEditRanges.Add Title:="Range1", Range:=Range( _
        "B11:AF180,AH11:AH180")
      
    ws.Protect pwd, DrawingObjects:=True, Contents:=True, Scenarios:=True
  
  
Next ws


End Sub
Sorry miss inputed range information

use this

VBA Code:
......
    ws.Protection.AllowEditRanges.Add Title:="Range1", Range:=Range( _
        "B11:AF180")
    ws.Protection.AllowEditRanges.Add Title:="Range2", Range:=Range( _
        "AH11:AH180")
.....
 
Upvote 0
Hey SunnyAlv,

Many thanks for the code!

I'll have to play around with it because it doesn't seem to be doing what I want.

Maybe it is something else on my workbook that is blocking it.

Any suggestions?

Thank you!
Pinaceous
 
Upvote 0
hmm, interesting, but try to check the section like the image below whether it contains it or not, and if there is, try deleting it, leave the inside of the box empty, and I just tried the VBA code that I created in a new excel sheet and it succeeded in locking all the cells in Excel except cell "B11 :AF180, AH11:AH180" as you mean

1700023302079.png
 
Upvote 1
SunnyAlv,

Ahh, thank you so much!

I'll be sure to try that!

Curious, do you know if there is a vba method to Allow users to edit ranges?

For example to blank the Allow the user to edit ranges through vba?

Thanks so much!
Pinaceous
 
Upvote 0

Forum statistics

Threads
1,215,077
Messages
6,122,991
Members
449,094
Latest member
masterms

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