Find last Row... Delete Empty Rows

buzz71023

Active Member
Joined
May 29, 2011
Messages
295
Office Version
  1. 2016
Platform
  1. Windows
I have a spreadsheet with a ton of data exported from another database.
Column H is either blank or has a dollar amount (could be positive or negative). If column H is blank, I want to delete that entire row.
I've done some research and came up with the below but it doesn't seem to be performing the way I am needing it to.
It is deleting empty rows but it is sporadic I want to delete them all in one click.
Need some help in getting this to work and trying to understand why this code is only deleting a portion of the data each time I execute the macro.


VBA Code:
Sub DeleteBlankVariances()
Dim rng As Range

lr = Cells(Rows.Count, 1).End(xlUp).Row

Set rng = Range("H2:H" & lr)

 For Each c In rng
        If c = "" Then c.EntireRow.Delete
    Next


End Sub
 
Almost certainly because you will have multiple consecutive blanks in column H. Suppose that rows 3 and 4 are blank in column H. Your code gets to row 3, deletes that row and moves to row 4. However, what was row 4 has moved up and is now row 3 and therefore misses its check for blank and hence is not deleted.
If deleting row-by-row you need to start from the bottom and work upwards.

However, you also said ..

There are several ways to achieve that but to advise an appropriate way for you, could your tell us
  1. about how many rows of data you are likely to have altogether before any rows are deleted, and
  2. about how many rows you expect to be blank in column H and need deleting.
Thanks for the reply.
1.) I have a header and then depending on the data, it could be the next row is the first is deleted. It could or will be different every time.
2.) This will also vary but could be as there could be as many as a couple hundred thousand blank rows. This is an export for for YTD data, so as the year progresses, the # of rows will grow. I can tell you there will be more blank cells in H than cells with data.
 
Upvote 0

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
It is not reliable. For example, with this sample data there should be the heading row and 2 other rows remaining but the code actually leaves the header row and 5 other rows!

buzz71023_1.xlsm
ABCDEFGH
1Hdr1Hdr2Hdr3Hdr4Hdr5Hdr6Hdr7Hdr8
211361229
3316957
414939951
5611552
6593691
74111111
Sheet2


As you alluded to, it may also alter the order of the remaining data.

For a very fast method that also retains the order of the remaining data and can accommodate any number of columns beyond column H ..

VBA Code:
Sub Del_Rws()
'  Delete rows where col H is blank
  Dim a As Variant, b As Variant
  Dim lr As Long, nc As Long, i As Long, k As Long
 
  lr = Cells.Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
  nc = Cells.Find(What:="*", LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
  a = Range("H2:H" & lr).Value
  ReDim b(1 To UBound(a), 1 To 1)
  For i = 1 To UBound(a)
    If Len(a(i, 1)) = 0 Then
      b(i, 1) = 1
      k = k + 1
    End If
  Next i
  If k > 0 Then
    Application.ScreenUpdating = False
    With Range("A2").Resize(UBound(a), nc)
      .Columns(nc).Value = b
      .Sort Key1:=.Columns(nc), Order1:=xlAscending, Header:=xlNo
      .Resize(k).EntireRow.Delete
    End With
    Application.ScreenUpdating = True
  End If
End Sub

Hi Peter,
This worked perfectly and very fast!
 
Upvote 0
there could be as many as a couple hundred thousand blank rows. ... I can tell you there will be more blank cells in H than cells with data.
Then you certainly don't want to use a row-by-row deletion method.

Hi Peter,
This worked perfectly and very fast!
Good news. It is certainly the fasted method that I have come across for when there is large data and lots of disjoint row ranges to be deleted.
 
Upvote 0

Forum statistics

Threads
1,224,504
Messages
6,179,142
Members
452,892
Latest member
JUSTOUTOFMYREACH

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