Delete all sheet except "Dashboard" sheet

gleamng

Board Regular
Joined
Oct 8, 2016
Messages
98
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2013
  6. 2011
  7. 2010
  8. 2007
  9. 2003 or older
Platform
  1. Windows
  2. MacOS
  3. Mobile
  4. Web
Good day everyone, sure you are all well.

Please kindly help me with this edited code, i got Run-time error 1004 - method delete of object _worksheet failed which highlight xWs.Delete


VBA Code:
Sub DeleteSheets()
    Dim xWs As Worksheet
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    For Each xWs In ThisWorkbook.Worksheets
        If xWs.Name <> "Dashboard" Then
            xWs.Delete
        End If
    Next xWs
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Is the workbook protected?

Is the sheet you want to save called 'Dashboard' 100% with no trailing spaces etc...?
 
Upvote 0
Hi,

You can test following
VBA Code:
Sub DeleteSheets()
    Dim xWs As Worksheet
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    For Each xWs In ThisWorkbook.Worksheets
         Select Case xWs.Name
            Case Is <> "DashBoard"
                xWs.Delete
         End Select
    Next xWs
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
End Sub
 
Upvote 0
Is the workbook protected?

Is the sheet you want to save called 'Dashboard' 100% with no trailing spaces etc...?

Thanks for your response.
The workbook is password protected which I've unprotect with this code (thisworkbook.unprotect password:="Gb1234")
And also the sheet to save is Dashboard without any space
 
Upvote 0
Hi,

You can test following
VBA Code:
Sub DeleteSheets()
    Dim xWs As Worksheet
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    For Each xWs In ThisWorkbook.Worksheets
         Select Case xWs.Name
            Case Is <> "DashBoard"
                xWs.Delete
         End Select
    Next xWs
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
End Sub

Thanks for your quick response.
I've tried the code change but i have the same error, meanwhile i use same code on another workbook and it worked. I don't know what is wrong with the major workbook i want it applies
 
Upvote 0
Can you delete the sheet manually at the point you would run the code?

I am asking as the only way I get the same error is if the workbook is protected.
 
Upvote 0
Also do you have any instances of 'On Error Resume Next' in your actual code?
 
Upvote 0

Forum statistics

Threads
1,223,914
Messages
6,175,351
Members
452,638
Latest member
Oluwabukunmi

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top