VBA Protect worksheet (1 Viewer)

Rania01

Member
Local time
Today, 21:23
Joined
Oct 9, 2021
Messages
59
Dear All,
I'm attempting to protect the entire worksheet except for a set column "H" can be editing
I use below vba the problem is all entire worksheet is protect please help how to :

Code:
Sub Lock_data()

    Dim ws As Worksheet

    For Each ws In ActiveWorkbook.Worksheets

    ws.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
        , AllowFormattingColumns:=True, AllowFormattingRows:=True, Password:="12345"
            
    Next ws

End Sub
 

Gasman

Enthusiastic Amateur
Local time
Today, 20:23
Joined
Sep 21, 2011
Messages
14,380
Use the Macro recorder to protect a range and see what that produces, then mimic in your code.
You might have to do A:G and I:whatever separately.

See if you can protect all the sheet, then unprotect a column.

Edit: Just had a quick peek, and seems sheet only?
 
Last edited:

CJ_London

Super Moderator
Staff member
Local time
Today, 20:23
Joined
Feb 19, 2013
Messages
16,639
as gasman says use the macro recorder. Note that cells have the locked property set to true as the default

  1. start the recorder
  2. select the cells you want users to be able to access
  3. on the Home ribbon select the Format button in the cells section and then select format cells
  4. select the protection tab and untick the Locked option
  5. select the format button again and this time select protect sheet.
  6. Choose whatever options you want the user to be able to do - typically select locked and unlocked cells. They won't be able to change the values in locked cells.
  7. add a password

Note that a user cannot adjust column widths or row heights in a protected worksheet so you will need to ensure columns are a suitably sized.
 

Rania01

Member
Local time
Today, 21:23
Joined
Oct 9, 2021
Messages
59
Dear Gasman,

I tried to use the Macro see below, the question is where can i put a password?


Code:
Sub Macro3()
'
' Macro3 Macro
'

'
    Columns("A:G").Select
    ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
        , AllowFormattingColumns:=True, AllowFormattingRows:=True
End Sub
 

Gasman

Enthusiastic Amateur
Local time
Today, 20:23
Joined
Sep 21, 2011
Messages
14,380
Last edited:

Users who are viewing this thread

Top Bottom