VBA to loop through worksheets

grapevine

Board Regular
Joined
May 23, 2007
Messages
208
I am trying to write a macro to look at cell A2 in each sheet in a workbook, and if there is no data in this cell then to delete the worksheet. (so I am left with only the sheets with comprehensive data on). I am looking at cell A2 not A1 as there is likely to be residual date in row 1.

The code I have tried to write which is obviously not working and for which I need help is below. Any help as to what else i need to include or a better way of doing this task would be gratefully received. (The reason I am trying to automate this seemingly simple task is that it will be one small part of a much larger macro.)

Happy christmas and thanking you in advance.

Sub DeleteSheets()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
If IsEmpty("A2") Then
ActiveSheet.Delete
End If
On Error Resume Next

Next ws
End Sub
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Maybe:

Code:
Sub doit()

Application.DisplayAlerts = False

Dim i As Integer

x = Sheets.Count

For i = x To 1 Step -1
    If IsEmpty(Sheets(i).Range("A2")) Then
        Sheets(i).Delete
    End If
Next i

Application.DisplayAlerts = True

End Sub
 
Upvote 0
Try:

Code:
Sub DeleteSheets()
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
        If ws.Range("A2") = "" Then
            ws.Delete
        End If
    Next ws
End Sub
 
Upvote 0
I think the fundamental error in your code is where you have:

Code:
ActiveSheet.Delete

should be:

Code:
ws.Delete

Also, Andrew's use of "" instead of IsEmpty is prolly a better method to use.

And turning off DisplayAlerts will make sure that you are not prompted with a warning each time a sheet is deleted.

Regards
Jon
 
Upvote 0
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
ws.Activate

If Range("a2") = "" Then
ActiveSheet.Delete

End If

Next ws
 
Upvote 0
Thank you all so much for your suggestions, I am now working and deleting sheets as required.

Happy christmas to you all
 
Upvote 0

Forum statistics

Threads
1,223,247
Messages
6,171,007
Members
452,374
Latest member
keccles

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