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
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Works for me. Is it possible some cells in H have a space that makes it appear the cell is empty?
 
Upvote 0
trying to understand why this code is only deleting a portion of the data each time I execute the macro.
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 ..
I want to delete them all in one click.
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.
 
Upvote 1
Hi
Unless"as kweaver Q"
Try this code and see if it suits you
VBA Code:
Sub test()
ActiveSheet.Columns(8).SpecialCells(4).EntireRow.Delete
End Sub
 
Upvote 1
Hello Buzz,

If you don't mind sorting the data, using the Autofilter as follows will, in an instant, delete blank rows:

VBA Code:
Sub TestDelete()

    Dim ws As Worksheet: Set ws = Sheets("Your sheet name here") '----> Add your sheet name.
    
    Application.ScreenUpdating = False
    
    ws.Range("A2", ws.Range("H" & ws.Rows.Count).End(xlUp)).Sort ws.[A2], 1
    
    With ws.Range("H1", ws.Range("H" & ws.Rows.Count).End(xlUp))
            .AutoFilter 1, ""
            .Offset(1).EntireRow.Delete
            .AutoFilter
    End With
    
    Application.ScreenUpdating = True

End Sub

This test code relates to Columns A - H. If there are more columns in your data set, then change 'H' to suit in the sort part of the code.

I hope that this helps.

Cheerio,
vcoolio.
 
Upvote 1
@ vcoolio
You might do away with the line
VBA Code:
.Offset(1).EntireRow.Delete
By
VBA Code:
 .AutoFilter 1, "<>"
 
Upvote 0
If you don't mind sorting the data, using the Autofilter as follows will, in an instant, delete blank rows:
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
 
Upvote 0
Solution

Forum statistics

Threads
1,224,504
Messages
6,179,144
Members
452,891
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