Delete Extra/Blank Sheets

Murphander

New Member
Joined
Nov 29, 2016
Messages
9
Hello - I wrote the following code to delete all empty sheets in a workbook (leaving one). I'm not an expert by any means, just wrote it the best way I knew how. Could you let me know if there is a better/more efficient way to do this?


Code:
Dim ws As Worksheet


    For Each ws In ActiveWorkbook.Worksheets
        If ActiveWorkbook.Sheets.count > 1 Then
            If WorksheetFunction.CountA(ws.Cells) = 0 Then
                If ws.Pictures.count = 0 And ws.ChartObjects.count = 0 Then
                    ws.Delete
                End If
            End If
        End If
    Next ws

If only blanks cells with no images/charts, then delete sheet.

Any thoughts?
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Seems fine. Only time it will fall down is when all sheets in a workbook are blank
 
Upvote 0
Thanks gallen, good call.

Though when testing I've never had an issue with this. If all sheets are blank and I run this it will leave one sheet, even though it's blank. Maybe this is a default Excel behavior? Would I need to add some sort of validation to this routine to account for this or leave it as is?
 
Upvote 0

Forum statistics

Threads
1,203,670
Messages
6,056,664
Members
444,881
Latest member
Stu2407

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