Removing blank/empty rows

default_name

Board Regular
Joined
May 16, 2018
Messages
180
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
I have a workbook that contains several different sheets of data.
Two sheets, in particular, occasionally end up being unnecessarily long at the bottom/end of the sheet.
There are many instances where the blank rows extend well beyond the data.
This has increased my file size dramatically, making it much more difficult to work with.

I am trying to implement code that will go through each of these worksheets, and remove all of the blank rows that are coming after the data.

Here is what I have right now. I am thinking that there must be a more simple way to do this, because when I run this code it takes forever to run, and it usually ends up crashing Excel in the end.

VBA Code:
    Sheets("Data1").Select
    Dim x As Long
    With ActiveSheet
        For x = .Cells.SpecialCells(xlCellTypeLastCell).Row To 1 Step -1
            If WorksheetFunction.CountA(.Rows(x)) = 0 Then
                ActiveSheet.Rows(x).Delete
            End If
        Next
    End With
  
    Sheets("Data2").Select
    Dim y As Long
    With ActiveSheet
        For y = .Cells.SpecialCells(xlCellTypeLastCell).Row To 1 Step -1
            If WorksheetFunction.CountA(.Rows(y)) = 0 Then
                ActiveSheet.Rows(y).Delete
            End If
        Next
    End With

Thanks in advance
 
Are there any columns that will always have data on every row (not formulae)?
On worksheet Data1 there is always data on every row in Column A
On worksheet Data2 there is always data on every row in Column BH

I assume you ask so that you can locate the bottom of the data, right?
 
Upvote 0

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
That's right. Is it just these two sheets?
I was mainly focused on Data1 and Data2 but as long as we're here, might as well apply it to the others.

On worksheet Data1 there is always data on every row in Column A
On worksheet Data2 there is always data on every row in Column BH
On worksheet Analyze1 there is always data on every row in Column A
On worksheet Analyze2 there is always data on every row in Column A
 
Upvote 0
Ok, how about
VBA Code:
Sub defaultname()
   Dim Ary As Variant
   Dim i As Long
   
   Ary = Array("Data1", "Analyze1", "Analyze2")
   For i = 0 To UBound(Ary)
      With Sheets(Ary(i))
         .Range(.Range("A" & Rows.Count).End(xlUp).Offset(1), .Range("A" & Rows.Count)).EntireRow.Delete
      End With
   Next i
   With Sheets("Data2")
      .Range(.Range("Bh" & Rows.Count).End(xlUp).Offset(1), .Range("BH" & Rows.Count)).EntireRow.Delete
   End With
End Sub
 
Upvote 0
Glad to help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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