Share via

combining 2 VBAs - Protect worksheets / edit objects

Anonymous
2017-10-18T17:47:55+00:00

Below are the 2 codes i'm working with. One password protects all the worksheets and the other allows all worksheets to edit a drawing object.

I am trying to combine the 2 into 1. Is anyone able to help?

_________

'Protecting worksheets

Sub protect_all_sheets()

top:

pass = InputBox("password?")

repass = InputBox("Verify Password")

If Not (pass = repass) Then

MsgBox "you made a mistake"

GoTo top

End If

For I = 1 To Worksheets.Count

If Worksheets(I).ProtectContents = True Then GoTo oops

Next

For Each s In ActiveWorkbook.Worksheets

s.Protect Password:=pass

Next

Exit Sub

oops: MsgBox "I think you have some sheets that are already protected. Please unprotect all sheets then running this Macro."

End Sub

_______

'Editing objects in protected workbooks

Sub edit_objects2()

Dim WS_Count As Integer

Dim I As Integer

WS_Count = ActiveWorkbook.Worksheets.Count

Application.ScreenUpdating = False

For I = 1 To WS_Count

On Error Resume Next

With Worksheets(ActiveWorkbook.Worksheets(I).Name)

.EnableOutlining = True

.EnableAutoFilter = True

.Protect Password:="123", _

Contents:=True, DrawingObjects:=False, UserInterfaceOnly:=True, _

AllowFormattingCells:=True

End With

Next I

Application.ScreenUpdating = True

End Sub

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2017-10-18T20:18:55+00:00

If you want to allow editing objects with the first macro, then try this:

'Protecting worksheets

Sub protect_all_sheets()

top:

pass = InputBox("password?")

repass = InputBox("Verify Password")

If Not (pass = repass) Then

    MsgBox "you made a mistake"

    GoTo top

End If

For I = 1 To Worksheets.Count

    If Worksheets(I).ProtectContents = True Then GoTo oops

Next

For Each s In ActiveWorkbook.Worksheets

    s.Protect Password:=pass, Contents:=True, DrawingObjects:=False, UserInterfaceOnly:=True, AllowFormattingCells:=True

Next

Exit Sub

oops: MsgBox "I think you have some sheets that are already protected. Please unprotect all sheets then running this Macro."

End Sub

Was this answer helpful?

2 people found this answer helpful.
0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2017-10-19T13:33:19+00:00

    Thank you teylyn! works perfectly

    Was this answer helpful?

    0 comments No comments