You need error handling in your functions:
Function SHEETNAME(IndexNumber As Integer)
Application.Volatile
With ThisWorkbook
If IndexNumber <= .Sheets.Count Then
SHEETNAME = .Sheets(IndexNumber).Name
Else
SHEETNAME = ""
End If
End With
End Function
for example. You can also simplify the deletion code:
Sub Delete_Sheets()
'Delete All sheets, except for the first number of admin sheets
'as defined on the instructions page
If MsgBox("Are you sure you want to delete ALL Test Scripts?", vbYesNo, "Delete All?") = vbNo Then
MsgBox "Cancelled"
Exit Sub
End If
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
End With
Dim Strtsht As Integer
Dim Lstsht As Integer
Strtsht = Range("admin_sheets").Value
Lstsht = Sheets.Count
' check to see if there any sheets to delete
If Lstsht <= Strtsht Then
MsgBox "There are No Additional Sheets to Delete"
Else
Application.DisplayAlerts = False
' loop through sheets and delete
For i = Lstsht To Strtsht + 1 Step -1
Sheets(i).Delete
Next i
MsgBox "Deletion Complete."
End If
With Application
.DisplayAlerts = True
.Calculation = xlCalculationAutomatic
End With
End Sub
Bookmarks