Help with VAB unprotecting sheets

Herpe_derp

New Member
Joined
Jun 21, 2023
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Hello

My code is set to wipe a workbook based on a date

Private Sub Workbook_Open()

Dim ws As Worksheet

' Check to see if specified date is passed
If Date >= DateSerial(2023, 7, 6) Then
' If it is, loop through all sheets
For Each ws In ActiveWorkbook.Worksheets
' Clear contents of each sheet
ws.Cells.ClearContents
Next ws
' Save workbook so data is permanently gone
ActiveWorkbook.Save
End If

End Sub

However, this is struggling to work on password protected sheets. Is there any way it can be modified to unprotect sheets before running?

The debug highlights ws.Cells.ClearContents yellow
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Try this...

It should work for sheets that are protected without a password. It will unprotect all sheets.

VBA Code:
Sub RemoveProtection()
'
' This macro removes protection from all sheets
'

'
Dim WSheet As Worksheet
    For Each WSheet In Worksheets
    If WSheet.ProtectContents = True Then
    WSheet.Unprotect
    Else: GoTo x
End If
Next WSheet
    
    'ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
x:
End Sub
 
Upvote 0
Thanks for this however, the sheets are password protected.

Could I also have help combining both macros into one?

Thanks

Try this...

It should work for sheets that are protected without a password. It will unprotect all sheets.

VBA Code:
Sub RemoveProtection()
'
' This macro removes protection from all sheets
'

'
Dim WSheet As Worksheet
    For Each WSheet In Worksheets
    If WSheet.ProtectContents = True Then
    WSheet.Unprotect
    Else: GoTo x
End If
Next WSheet
   
    'ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
x:
End Sub
 
Upvote 0
About ten sheets in one workbook, 3 of which are password protected all with the same password
Try this :

VBA Code:
Option Explicit

Private Sub Workbook_Open()

    Dim oWsh As Worksheet
    
    If Date >= DateSerial(2023, 7, 6) Then
        For Each oWsh In Me.Worksheets
            With oWsh
                If .ProtectContents Then
                    .Unprotect Password:="YourPassword"
                    .Cells.ClearContents
                    .Range("A1").ID = "Protected"
                End If
                If .Range("A1").ID = "Protected" Then
                    .Protect Password:="YourPassword"
                End If
            End With
        Next
        Me.Save
    End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,125
Members
453,021
Latest member
Justyna P

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