Delete Empty Worksheets

MattMax

New Member
Joined
Nov 13, 2009
Messages
34
Hello,

I would like to create a macro that searches a workbook to determine how many worksheets contain data. It will delete the empty worksheets and record in an array, the position of the active worksheets.

So far I'm getting stuck on the delete function
"A workbook must contain at least one visible worksheet"

Any help would be great. Thanks!


Sub Find_worksheets()

Dim shtNext As Worksheet
Dim WorkSheetArray(0 To 9)

worksheetnumber = ActiveWorkbook.Worksheets.Count
MsgBox "There are " & worksheetnumber & " Worksheets"

For Each shtNext In ActiveWorkbook.Worksheets
With shtNext.Range("AD2")
If .Value > 0 Then
WorkSheetArray(shtNext) = shtNext
Else
ActiveWorkbook.Worksheets.Delete
End If
End With
Next shtNext

End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
The error message is explicit. We must have at least 1 sheet in a workbook.
If all sheets are empty why keep the workbook ?

Here is some code.
Code:
'=============================================================================
'- DELETE WORKSHEETS WITH EMPTY CELL D2
'=============================================================================
Sub test()
    Application.DisplayAlerts = False       ' to stop delete messages
    On Error Resume Next                    ' to trap error
    '-------------------------------------------------------------------------
    For Each ws In ActiveWorkbook.Worksheets
        If ws.Range("D2").Value = "" Then
            ws.Delete
            '-----------------------------------------------------------------
            '- TRAP DELETE ERROR
            If Err.Number <> 0 Then
                Err.Clear
                MsgBox ("Cannot delete sheet '" & ws.Name & " '")
                Exit Sub
            End If
            '----------------------------------------------------------------
        End If
    Next
    '========================================================================
    '- NB. MAKE SURE THAT THIS LINE RUNS OR WILL GET NO FURTHER MESSAGES
    Application.DisplayAlerts = True
    '========================================================================
End Sub

====================================================
NB. DisplayAlerts must be used with extreme care. It stops all messages (in this case the prompt to confirm deletion).

Must run the line of code to reset to true.

Code:
    Application.DisplayAlerts = True
'=======================================================
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,683
Members
449,116
Latest member
HypnoFant

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