Deleting multiple worksheets and saving active worksheet

singcbl

Well-known Member
Joined
Feb 8, 2006
Messages
518
I am trying to find a easy way to delete multiple worksheets in a workbook and then saving the workbook based on the tab name of the worksheet.
 
Jindon-san,

This error message appeared at this position

Method 'Delete object'_Worksheet' failed

If ws.Name <> NewName Then ws.Delete
 
Upvote 0

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
It is not selecting/activating the object, so it shouldn't be the issue.

Isn't the workbook protected?
 
Upvote 0
Jindon-san,

Yes, it is protected but when I modify your codes as follows it seem to work

Code:
Dim myPath As String, NewName As String, ws As Worksheet
myPath = ThisWorkbook.Path
NewName = ActiveSheet.Name
Application.DisplayAlerts = False
For Each ws In Sheets
ws.Visible = True
     If ws.Name <> NewName Then ws.Delete
Next
ThisWorkbook.SaveAs NewName & ".xls"
 
Upvote 0
Jindon-san,

I have to correct my statement. I need to have the hidden sheets left hidden because I need these sheets as they are linked to the active sheet. If this the case how to change the code to overcome the error
 
Upvote 0
Jindon,

Of the hidden sheets, I just need to make the sheet "Template" visible and delete with the rest of the sheets except the active sheet
 
Upvote 0
Jindon-san,

Finally found a way round the problem of hidden files. I managed to find the solution and change the codes as follows.

Code:
Sub exportwksht()
    Dim myPath As String, NewName As String, ws As Worksheet
    myPath = ThisWorkbook.Path
    NewName = ActiveSheet.Name
    Application.DisplayAlerts = False
    For Each ws In Sheets
    On Error Resume Next
    If ws.Name <> NewName Then ws.Delete
    Next ws
    On Error GoTo 0
    Application.DisplayAlerts = True
    ThisWorkbook.SaveAs NewName & ".xls"
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,582
Members
449,039
Latest member
Arbind kumar

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