combine these two macro's into one macro

Prasad K

Board Regular
Joined
Aug 4, 2021
Messages
189
Office Version
  1. 2016
  2. 2007
Platform
  1. Windows
Hi Experts

can anyone combine these 2 VBA macro's into 1 macro

one macro will protect all worksheet with an inputbox

second macro will unprotect all worksheet with just given password



VBA Code:
Sub protect_all_sheets()
top:
pass = InputBox("password?")
repass = InputBox("Verify Password")
If Not (pass = repass) Then
MsgBox "you made a boo boo"
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, DrawingObjects:=False, Contents:=True, Scenarios:=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

Sub unprotect_all_ws()
Dim ws As Worksheet
For Each ws In Worksheets
ws.UnProtect "123"
Next ws
End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Maybe
VBA Code:
Sub protect_all_sheets()
pass = InputBox("password?")
repass = InputBox("Verify Password")
If Not (pass = repass) Then
MsgBox "you made a boo boo"
Else
For Each ws In Worksheets
ws.Unprotect pass
Next ws
End If
For Each s In ActiveWorkbook.Worksheets
s.Protect Password:=pass, DrawingObjects:=False, Contents:=True, Scenarios:=True
Next s
End Sub
 
Upvote 0
Maybe
VBA Code:
Sub protect_all_sheets()
pass = InputBox("password?")
repass = InputBox("Verify Password")
If Not (pass = repass) Then
MsgBox "you made a boo boo"
Else
For Each ws In Worksheets
ws.Unprotect pass
Next ws
End If
For Each s In ActiveWorkbook.Worksheets
s.Protect Password:=pass, DrawingObjects:=False, Contents:=True, Scenarios:=True
Next s
End Sub
Michael its just protecting the sheet when i run this code and again run this code to unprotect sheet its not unprotecting
 
Upvote 0
What are you actually trying to do...
The 2 codes provided simply UnProtect then reProtect with no action in between !!
VBA Code:
Sub protect_all_sheets()
pass = InputBox("password?")
repass = InputBox("Verify Password")
If Not (pass = repass) Then
MsgBox "you made a boo boo"
Else
For Each ws In Worksheets
ws.Unprotect pass
Next ws
MsgBox "All sheets are now Unprotected."
End If
For Each s In Worksheets
s.Protect Password:=pass, DrawingObjects:=False, Contents:=True, Scenarios:=True
Next s
End Sub
 
Upvote 0
See if this does what you want.

VBA Code:
Sub SwitchProtection()
  Dim ws As Worksheet
  Dim pCount As Long
  Dim pass As String
  
  With ActiveWorkbook
    For Each ws In .Worksheets
      pCount = pCount - ws.ProtectContents
    Next ws
    If pCount > 0 And pCount < .Worksheets.Count Then
      MsgBox "I think you have some sheets that are already protected and some not. Please unprotect all sheets then run this Macro."
    Else
      If pCount = .Worksheets.Count Then
        For Each ws In .Worksheets
          ws.Unprotect "123"
        Next ws
      Else
        pass = InputBox("password?")
        If pass <> "123" Then
          MsgBox "You made a boo boo. Try again"
        Else
          For Each ws In .Worksheets
            ws.Protect Password:=pass, DrawingObjects:=False, Contents:=True, Scenarios:=True
          Next ws
        End If
      End If
    End If
  End With
End Sub
 
Upvote 0
Solution
What are you actually trying to do...
The 2 codes provided simply UnProtect then reProtect with no action in between !!
VBA Code:
Sub protect_all_sheets()
pass = InputBox("password?")
repass = InputBox("Verify Password")
If Not (pass = repass) Then
MsgBox "you made a boo boo"
Else
For Each ws In Worksheets
ws.Unprotect pass
Next ws
MsgBox "All sheets are now Unprotected."
End If
For Each s In Worksheets
s.Protect Password:=pass, DrawingObjects:=False, Contents:=True, Scenarios:=True
Next s
End Sub
i have given 2 codes to combine into 1 code

like i want a single macro to protect and unprotect sheets in a workbook with an inputbox with given password

you have given me a code is working to protecting sheets in a workbook
 
Upvote 0
See if this does what you want.

VBA Code:
Sub SwitchProtection()
  Dim ws As Worksheet
  Dim pCount As Long
  Dim pass As String
 
  With ActiveWorkbook
    For Each ws In .Worksheets
      pCount = pCount - ws.ProtectContents
    Next ws
    If pCount > 0 And pCount < .Worksheets.Count Then
      MsgBox "I think you have some sheets that are already protected and some not. Please unprotect all sheets then run this Macro."
    Else
      If pCount = .Worksheets.Count Then
        For Each ws In .Worksheets
          ws.Unprotect "123"
        Next ws
      Else
        pass = InputBox("password?")
        If pass <> "123" Then
          MsgBox "You made a boo boo. Try again"
        Else
          For Each ws In .Worksheets
            ws.Protect Password:=pass, DrawingObjects:=False, Contents:=True, Scenarios:=True
          Next ws
        End If
      End If
    End If
  End With
End Sub
Yes this one i should asking

Thank you Peter
 
Upvote 0

Forum statistics

Threads
1,214,415
Messages
6,119,375
Members
448,888
Latest member
Arle8907

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