I have code meant to list hidden worksheets, unhide them, remove a password, list workbooks that the file is linked to, refresh a power query data connection, re-apply the password, and hide previously hidden sheets as well as any sheets that are colored green on workbook open.
It causes Excel to crash. I tried modifying the code numerous times, removing the code from the workbook, saving the workbook as .XLS then re-opening, adding the code back in and re-saving as .XLSM.
Any suggestions why this may be happening or how I can improve the code to prevent Excel from crashing?
Private Sub Workbook_Open()
'Place in ThisWorkbook to run code on Workbook_Open
'Ensure that Consolidated - Query does NOT have Background Refresh Enabled in Query Properties
Dim x As Long
Dim shtCnt As Integer
shtCnt = ThisWorkbook.Sheets.Count
Application.StatusBar = "Setting up for volume refresh..."
Application.Calculation = xlCalculationManual
Sheets("Control").Visible = True
Sheets("Control").Activate
ActiveSheet.Unprotect Password:="passwordhere"
Sheets("Control").Select
'clear out old list
Sheets("Control").Range("T7").Value = "Hidden Worksheets:"
Range("T7").Select
Selection.Font.Bold = True
Selection.Font.Underline = True
Range("T8:T5000").Select
Selection.Clear
'list hidden sheets
On Error Resume Next
x = 8
For i = 1 To shtCnt
If Sheets(i).Visible = xlSheetHidden Then
Cells(x, 20) = Sheets(i).Name
x = x + 1
End If
Next i
'unhide hidden sheets
stp = Worksheets("Control").Range("T8:T5000").Cells.SpecialCells(xlCellTypeConstants).Count
y = 8
For j = 1 To stp
Sheets(Cells(y, 20).Value).Visible = True
y = y + 1
Next j
For i = 1 To Sheets.Count
With Sheets(i)
.Unprotect Password:="password"
.Outline.ShowLevels RowLevels:=1
End With
Next i
'list linked workbooks path
Application.StatusBar = "Refreshing volume..."
Dim wb As Workbook
Set wb = Application.ThisWorkbook
Sheets("Control").Range("T4").Activate
If Not IsEmpty(wb.LinkSources(xlExcelLinks)) Then
xIndex = 4
For Each link In wb.LinkSources(xlExcelLinks)
If Not link Like "*Corporate Guidelines Master.xlsm" Then
Application.ActiveSheet.Cells(xIndex, 20).Value = link
xIndex = xIndex + 1
End If
Next link
End If
'refresh volume query
Application.Calculation = xlCalculationAutomatic
ThisWorkbook.Connections("Query - Consolidated").Refresh
Application.Wait (Now + TimeValue("0:00:02"))
DoEvents
Application.StatusBar = "Please wait..."
For i = 1 To Sheets.Count
With Sheets(i)
.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowFormattingColumns:=True, AllowFormattingRows:=True _
, Password:="passwordhere"
.Select
Cells(ThisWindow.SplitRow + 1, ThisWindow.SplitColumn + 1).Select
End With
Next i
'hide originally hidden sheets
Dim tc As Object
For Each tc In ThisWorkbook.Sheets
If tc.Tab.Color = 4697456 Then
tc.Visible = xlSheetHidden
End If
Next tc
stpend = Worksheets("Control").Range("T8:T5000").Cells.SpecialCells(xlCellTypeConstants).Count
Z = 8
Sheets("Control").Range("T8").Select
For k = 1 To stpend
Sheets(Cells(Z, 20).Value).Visible = False
Z = Z + 1
Next k
'close out
Sheets("Control").Visible = False
Sheets("Plant Summary Graphs").Select
Range("A1").Activate
Application.StatusBar = False
End
End Sub
On Error Resume Nextand see if any error came out.on error resum nextas Raymond suggests, I would create subs for each task (unhiding/unprotecting the sheets, listing linked workbook paths etc.). Than you can test each routine itself it it's the part that leads to the crash. In the end you will have a main sub calling one task after the other.On Error Resume Nextto be there you should putOn Error Goto 0after the part you need to skip error checking