Page tree

Welcome to FreeSoftwareServers Confluence Wiki

Skip to end of metadata
Go to start of metadata
Public Sub AutoFitAllWSinWB()
Dim WSPWDProtected As Boolean
For Each ws In ActiveWorkbook.Sheets
 WSPWDProtected = False
 ws.Activate
If ActiveSheet.ProtectContents = False Then
 Cells.Columns.AutoFit
Else
 WSPWDProtected = isSheetProtectedWithPassword(Worksheets(ws.Name))
 If WSPWDProtected = False Then
  ActiveSheet.Unprotect
  Cells.Columns.AutoFit
  ActiveSheet.Protect
 End If
End If
Next ws
End Sub
Function isSheetProtectedWithPassword(ws As Worksheet) As Boolean
    If ws.ProtectContents Then
        On Error GoTo errorLabel
        ws.Unprotect ""
        ws.Protect
    End If
errorLabel:
    If Err.Number = 1004 Then isSheetProtectedWithPassword = True
End Function
  • No labels