VBA Protect worksheet

Rania01

Member
Local time
Today, 07:49
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
 
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:
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.
 
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
 
Last edited:

Users who are viewing this thread

Back
Top Bottom