Welcome to FreeSoftwareServers Confluence Wiki

Public Sub AutoFitWS()
 If ActiveSheet.ProtectContents = False Then
   Call Do_AutoFit
 Else
 WSPWDProtected = isSheetProtectedWithPassword(ActiveSheet)
  If WSPWDProtected = False Then
   ActiveSheet.Unprotect
   Call Do_AutoFit
   ActiveSheet.Protect
  End If
 End If
End Sub
Public Sub Do_AutoFit()
   Cells.Select
   Selection.ColumnWidth = 100
   Cells.EntireColumn.AutoFit
   Cells.EntireRow.AutoFit
   lCol = Get_lCol(ActiveSheet)
   Range(Cells(1, lCol + 1), Cells(Rows.Count, Columns.Count)).ColumnWidth = 22
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