Removing blank/empty rows

default_name

Board Regular
Joined
May 16, 2018
Messages
153
Office Version
  1. 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
 

default_name

Board Regular
Joined
May 16, 2018
Messages
153
Office Version
  1. 2016
Platform
  1. Windows
  2. MacOS
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?
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result

default_name

Board Regular
Joined
May 16, 2018
Messages
153
Office Version
  1. 2016
Platform
  1. Windows
  2. MacOS
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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,695
Office Version
  1. 365
Platform
  1. Windows
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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,695
Office Version
  1. 365
Platform
  1. Windows
Glad to help & thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,129,842
Messages
5,638,676
Members
417,041
Latest member
Molo

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
Top