How to Delete Excel Tabs where there is less than 2 rows of Data

Vichet

New Member
Joined
Mar 12, 2017
Messages
1
Hi,
I've been successfully able to delete all excel tabs where there are 0 rows of data in it using the code below:
Sub DelMT()
Dim ws As Worksheet
Application.DisplayAlerts = False
For Each ws In ActiveWorkbook.Worksheets
If Application.CountA(ws.Cells) = 0 Then ws.Delete
Next ws
Application.DisplayAlerts = True
End Sub

However, I like to delete all tabs where there is less than 1 row. i.e. If you have only 1 or more cells in the first row of a given sheet that sheet will be deleted.
If you have two or more rows, that sheet would not get deleted.
What changes are required to the above code?
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Welcome to the MrExcel board!

Somewhat confusing.

Title: How to Delete Excel Tabs where there is less than 2 rows of Data
Post #1: I like to delete all tabs where there is less than 1 row
Post #1: If you have only 1 or more cells in the first row of a given sheet that sheet will be deleted

Anyway, assuming that column A in each sheet can be used to determine how many rows have been used, try the code below in a copy of your workbook.
Note that a workbook cannot exist with zero sheets so if all worksheets have less than 2 rows, one sheet will still remain after the code.

Rich (BB code):
Sub Delete_Sheets()
  Dim ws As Worksheet
  
  Application.DisplayAlerts = False
  For Each ws In ActiveWorkbook.Worksheets
    If ws.Cells(ws.Rows.Count, "A").End(xlUp).Row = 1 And ActiveWorkbook.Sheets.Count > 1 Then ws.Delete
  Next ws
  Application.DisplayAlerts = True
End Sub

If column A is not a reliable guide to the number of rows used then post back for an alternative code
 
Upvote 0

Forum statistics

Threads
1,214,991
Messages
6,122,628
Members
449,095
Latest member
bsb1122

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