Deleting empty rows fastest way possible

adnan1975

New Member
Joined
Aug 24, 2017
Messages
38
I am sure this has been discussed many times before but here it is again. I am trying to delete empty rows from a spreadsheet with more than 10000 rows. I am using the following code but it doesn't work.

VBA Code:
Sub RowDeleter()
    Dim sht As Worksheet
    Dim r As Long
    Dim EndRow As Long
    Dim TCount As Long
    Dim s As Date
    Dim e As Date

    Application.ScreenUpdating = True
    r = 2       'Initialise row number
    s = Now     'Start Time
    Set sht = ActiveSheet
    EndRow = sht.Cells.SpecialCells(xlCellTypeLastCell).Row

    'Check if "Test String" is found in Column 1
    TCount = Application.WorksheetFunction.CountIf(sht.Columns(1), "")
    If TCount > 0 Then

        'loop through to the End row
        While r <= EndRow
            If InStr(sht.Cells(r, 1).Text, "") > 0 Then
                sht.Rows(r).Delete Shift:=xlUp
                r = r - 1
            End If
            r = r + 1
        Wend
    End If
    e = Now  'End Time
    D = (Hour(e) * 360 + Minute(e) * 60 + Second(e)) - (Hour(s) * 360 + Minute(s) * 60 + Second(s))
    Application.ScreenUpdating = True
    DurationTime = TimeSerial(0, 0, D)
    MsgBox Format(DurationTime, "hh:mm:ss")
End Sub

SegmentCountryProductDiscount BandUnits SoldManufacturing PriceSale PriceGross SalesDiscounts
GovernmentCanadaCarreteraNone
1618.5​
$ 3.00$ 20.00$ 32,370.00$ -
GovernmentGermanyCarreteraNone
1321​
$ 3.00$ 20.00$ 26,420.00$ -
MidmarketFranceCarreteraNone
2178​
$ 3.00$ 15.00$ 32,670.00$ -
MidmarketGermanyCarreteraNone
888​
$ 3.00$ 15.00$ 13,320.00$ -
MidmarketMexicoCarreteraNone
2470​
$ 3.00$ 15.00$ 37,050.00$ -
GovernmentGermanyCarreteraNone
1513​
$ 3.00$ 350.00$ 529,550.00$ -
MidmarketGermanyMontanaNone
921​
$ 5.00$ 15.00$ 13,815.00$ -
Channel PartnersCanadaMontanaNone
2518​
$ 5.00$ 12.00$ 30,216.00$ -
GovernmentFranceMontanaNone
1899​
$ 5.00$ 20.00$ 37,980.00$ -
Channel PartnersGermanyMontanaNone
1545​
$ 5.00$ 12.00$ 18,540.00$ -
MidmarketMexicoMontanaNone
2470​
$ 5.00$ 15.00$ 37,050.00$ -
EnterpriseCanadaMontanaNone
2665.5​
$ 5.00$ 125.00$ 333,187.50$ -
Small BusinessMexicoMontanaNone
958​
$ 5.00$ 300.00$ 287,400.00$ -
GovernmentGermanyMontanaNone
2146​
$ 5.00$ 7.00$ 15,022.00$ -
EnterpriseCanadaMontanaNone
345​
$ 5.00$ 125.00$ 43,125.00$ -
MidmarketUnited States of AmericaMontanaNone
615​
$ 5.00$ 15.00$ 9,225.00$ -
GovernmentCanadaPaseoNone
292​
$ 10.00$ 20.00$ 5,840.00$ -
MidmarketMexicoPaseoNone
974​
$ 10.00$ 15.00$ 14,610.00$ -
Channel PartnersCanadaPaseoNone
2518​
$ 10.00$ 12.00$ 30,216.00$ -
GovernmentGermanyPaseoNone
1006​
$ 10.00$ 350.00$ 352,100.00$ -
Channel PartnersGermanyPaseoNone
367​
$ 10.00$ 12.00$ 4,404.00$ -
GovernmentMexicoPaseoNone
883​
$ 10.00$ 7.00$ 6,181.00$ -


Any help is highly appreciated.
I tried to use L2BB add in but it is blocked by excel and dont know how to unlock it.
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Maybe this way
VBA Code:
Sub Del_Rows()
  On Error Resume Next
  Range("A2", Range("A" & Rows.Count).End(xlUp)).SpecialCells(xlBlanks).EntireRow.Delete
End Sub
 
Upvote 0
I tried to use L2BB add in but it is blocked by excel and dont know how to unlock it.
Look at these:
Xl2bb got disable
xl2bb


Deleting empty rows fastest way possible
spreadsheet with more than 10000 rows.
With your sample data copied down 10,000 rows, on my machine ..

Post 3 code: 2.7 seconds
Below code: 0.06 seconds (ie 45 x faster)

VBA Code:
Sub Del_Empty()
  Dim a As Variant, b As Variant
  Dim nc As Long, i As Long, k As Long

  nc = Cells.Find(What:="*", LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
  a = Range("A2", Range("A" & Rows.Count).End(xlUp)).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
Both without sorting, on a 15 yr old laptop, with 11,000 rows.
Assumed Columns A to I.

1.3 seconds for following
Code:
Sub Or_So_Maybe()
Dim dataArr, i As Long, rng As Range
dataArr = Sheets("Sheet1").Range("A1:I" & Cells(Rows.Count, 9).End(xlUp).Row).Value
    For i = 2 To UBound(dataArr)
        If dataArr(i, 1) = 0 Then
            If Not rng Is Nothing Then
                Set rng = Union(rng, Cells(i, 1).EntireRow)
                    Else
                Set rng = Cells(i, 1).EntireRow
            End If
        End If
    Next i
If Not rng Is Nothing Then rng.Delete Shift:=xlUp
Set rng = Nothing
End Sub

and 0.2 seconds for following (here pasted in another sheet)
Code:
Sub Or_So_Maybe_2()
Dim dataArr1, dataArr2(), i As Long, j As Long, k As Long
j = 1
dataArr1 = Sheets("Sheet1").Range("A1:I" & Cells(Rows.Count, 9).End(xlUp).Row).Value
'MsgBox UBound(dataArr1, 2)
ReDim Preserve dataArr2(1 To UBound(dataArr1, 1), 1 To UBound(dataArr1, 2))
    For i = 1 To UBound(dataArr1)
        If Not dataArr1(i, 1) = "" Then
            For k = 1 To UBound(dataArr1, 2)
                dataArr2(j, k) = dataArr1(i, k)
            Next k
        j = j + 1
        End If
    Next i
Sheets("Sheet4").Cells(1, 1).Resize(UBound(dataArr2, 1), UBound(dataArr2, 2)) = dataArr2
End Sub
 
Upvote 0
1.3 seconds for following

and 0.2 seconds for following (here pasted in another sheet)
Without comparison to the other codes using the same data on the same machine, the numbers themselves don't mean much.

For me, on the same data I used earlier, your first code took 4.031 seconds.
The second code took 0.164 seconds. This is not directly comparable since it is not actually deleting rows and any formulas & some types of formatting would be lost. Whether that is relevant to the OP we don't know.

In any case, with my tests, it was still about 3 times slower than the post 5 code so for me wouldn't qualify for the fastest way. It may be different for you as I have found differences like that reported by members in the past.
 
Upvote 0
Hi Peter.
A top of the morning (I think in your part of the world) to you.
I just wanted to see what different ways of attacking the problem would do because I saw that times were published..
It is not in any way an attempt to be faster then the next person, just a comparison doing it different.
As you mentioned, the 2nd code's time will be different if deleting the existing data is incorporated.
 
Upvote 0
A top of the morning (I think in your part of the world) to you.
No, latish afternoon actually (about 5:30 pm) but thanks anyway. :)

Understand what you are saying though I would be interested to see the results if you timed the other codes since, as I mentioned, sometimes in the past other members have reported different codes as fastest/slowest compared to my timings.
 
Upvote 0
I will tomorrow Peter because it is way past my beauty sleep time (midnight)
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,208
Members
452,618
Latest member
Tam84

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