Delete all worksheets in a workbook that have a blank value in cell A2

VBAProIWish

Well-known Member
Joined
Jul 6, 2009
Messages
1,027
Office Version
  1. 365
Platform
  1. Windows
Hello All,

I have this code below that looks to the active worksheet and then deletes the worksheet if cell A2 is blank but it is only for the active worksheet and I want the code below to look at every sheet in the workbook and do the same thing. The workbook is a dynamic one so the names and number of worksheets can be different every time I run the code.

In other words, I need code that looks at cell A2 in every worksheet and deletes whichever worksheets have a blank value.

VBA Code:
'Sub delete_WSS_with_no_data()
Application.DisplayAlerts = False
If IsEmpty(Range("A2").Value) = True Then
    ActiveSheet.delete
End If
Application.DisplayAlerts = True
'End Sub

Thanks much!
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Try this:
VBA Code:
Sub MyDeleteSheets()

    Dim ws As Worksheet
   
    Application.DisplayAlerts = False
   
    For Each ws In Worksheets
        If IsEmpty(ws.Range("A2")) Then ws.Delete
    Next ws

    Application.DisplayAlerts = True
   
    MsgBox "Macro complete!"

End Sub

Note: Make sure you have at least one sheet that has something in cell A2. I think you will run into problems if you try to delete all sheets in a workbook!
 
Upvote 0
Solution
If A2 in all sheets is empty, you'll need safeguard because you can't delete all sheets. Like mentioned above.
Code:
Sub Try_So()
Dim i As Long
    For i = ThisWorkbook.Worksheets.Count To 1 Step -1
        If ThisWorkbook.Sheets.Count > 1 Then
            If Len(Worksheets(i).Cells(2, 1)) = 0 Then
                Application.DisplayAlerts = False
                    Worksheets(i).Delete
                Application.DisplayAlerts = True
            End If
        End If
    Next i
End Sub
 
Upvote 0
The first code worked! Thank you!

Jolivanes, your code gave me a "Subscript Out of Range" error on this line here...

VBA Code:
  If Len(Worksheets(i).Cells(2, 1)) = 0 Then

but I will never have a workbook where all worksheets have a blank value in cell A2.

I can see how that may be helpful in the future though.

Thank you!
 
Upvote 0
You are welcome.
Glad we were able to help!
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,954
Members
449,096
Latest member
Anshu121

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