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

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Hi Peter.
Sorry for being a little late.

Device name: Laptop
Processor: Intel(R) Core(TM) i7-4600U CPU @ 2.10GHz 2.70 GHz
Installed RAM: 16.0 GB
System type: 64-bit operating system, x64-based processor

My findings:
10,825 Rows total of which 902 Rows are empty.
With Union: 1.40 seconds
With Arrays Different Sheet: 0.14 seconds
With Arrays Same Sheet: 0.23 seconds
Michael M's code: 0.77 seconds
Peter SSs' code: 0.09 seconds
 
Upvote 0
BTW, if you use ScreenUpdating False/True, 3 macros change very slightly.
Union, no change
Arrays different sheet, no change
Arrays same sheet, 0.20 seconds
Michael M's code, 0.74 seconds
Peter SSs' code, 0.08 seconds
 
Upvote 0
Thanks for the updated information. (y)

BTW, if you use ScreenUpdating False/True, 3 macros change very slightly.
You have mine as one that changed. My code already has screen updating off/on covering the part of the code where anything on the sheet changes so any change in timing is coincidental, not related to screen updating.
 
Upvote 0
Re: Code already has ScreenUpdating False/True on your (Peter's) code.
Yes, changes are probably because every time you run it, it changes slightly.
 
Upvote 0

Forum statistics

Threads
1,215,438
Messages
6,124,873
Members
449,192
Latest member
MoonDancer

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