VBA - DeleteAllSheetsButOneNamedMacro
Gabriel Kovacs
VBA Developer, Automation of Excel, Outlook, Word (data processing, email distribution, document manipulation etc.)
(click here to get the code) Unchanging conditions are essential for successful data processing. The best way is to use only one control sheet with buttons and create all other sheets from scratch. When starting the file this SW deletes all sheets except for the sheet named "Macro". New sheets are then created using other parts of the program. (this programm is free to use, tested, works properly)
Private Sub Workbook_Open()
DeleteAllWsButWsMacroVer001.DeleteAllButMacro
End Sub
Sub DeleteAllButMacro()
Dim ws As Worksheet
Dim blnWsMacroExists As Boolean
blnWsMacroExists = False
'check if worksheet "Macro" exists
For Each ws In ThisWorkbook.Worksheets
If ws.Name = "Macro" Then
blnWsMacroExists = True
Exit For
End If
Next ws
'if worksheet "Macro" exists, then delete all others
If blnWsMacroExists Then
Application.DisplayAlerts = False 'disable warning messages
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Macro" Then
ws.Delete
End If
Next ws
Application.DisplayAlerts = True 'enable warning messages
Else
MsgBox "Worksheet 'Macro' not found. & vbcrlf" _
& "No sheets were deleted.", vbExclamation
End If
End Sub