Macro to Delete Blank Rows

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I need a macro to delete blank rows from row 2, but excluding the last 4 rows where there is blank rows between the data

See example below where the rows containing blank cells after #2 in column A must not be deleted

Your assistance in this regard will be most apreciated

Howard
East.newvehicles.xls
ABCDEFG
9313.0DITSLXF/SCAB19,245019,245Br21
942RANGER3.0DITXLT4X218,245018,245Br11
95
96
97TotalUnits2
98
99TotalValue37,490037,490
100
East.newvehicles
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Assuming your data starts in row 2, and by 'blank' cells, you mean column A is empty...

Code:
Sub Del_Rows()
Dim End_Row As Long, n As Long

End_Row = Range("A1").End(xlDown).Row

For n = End_Row To 2 Step-1
With Cells(n,1)
If .Value = "" Then .EntireRow.Delete
End With
Next n

End Sub
 
Upvote 0
Maybe try:

Code:
sub deleteAllBut4()
dim lr as long
lr = cells(rows.count,1).end(xlup).offset(-4,0).row
range("A2:A" & lr).specialcells(xlcelltypeblanks).entirerow.delete
End Sub
This is untested, so please try on a copy of your workbook, but I believe it should work.
 
Upvote 0
Hi Guys thanks for the help.

Nkimack's macro, deletes the blank cell in row 2, but not an sunsequent rows containing blank cells.

Shielrn's macro deletes all the rows except the first row

See example below. it would be appreciated if you could assist

Regards

Howard
Delete Blanks.xls
ABCD
1StockNoDescriptionBasiccostsDFA
2
31FOCUS1.6AMBIENTE5DR12,224344
42SPORT1.6ORIGINAL13,450340
53IKON1.4AMBIENTE10,385344
64FOCUS1.6AMBIENTE4DR12,816344
75RANGER3.0DXLT4X2S/CA19,732344
86FIESTA1.4BASE5DOOR9,8250
97RANGER2.5TDXL4X213,640344
10852.0ACTIVE5DRMAN12,350343
11921.3DYNAMIC5DR110,0380
12
13
14
1510BT-502.5TD4X2F/SCAB12,870357
16113.0DITSLXF/SCAB19,100353
East.newvehicles
 
Upvote 0
I did qualify my post by assuming your data started in row 2. Your sample starts in row 3, so try the following...

Code:
Sub Del_Rows()
    Dim End_Row As Long, n As Long

    End_Row = Range("A" & Rows.Count).End(xlUp).Row.Offset(-5, 0)

    For n = End_Row To 2 Step -1
        With Cells(n, 1)
            If .Value = "" Then .EntireRow.Delete
        End With
    Next n

End Sub
 
Upvote 0
I edited mine, as I didn't read the part about blank cells, but a loop is still not necessary:

Code:
sub deleteAllBut4()
dim lr as long
lr = cells(rows.count,1).end(xlup).offset(-4,0).row
range("A3:A" & lr).specialcells(xlcelltypeblanks).entirerow.delete
End Sub
Hope that helps.
 
Upvote 0
Hi Njimack

It now comes up with a Compile error: Invalid Qualifier

End_Row = Range("A" & Rows.Count).End(xlUp).Row.Offset(-5, 0)

It would be appreciated if you would amend your code

Regards

Howard
 
Upvote 0
I think you may need to change this:

Code:
End_Row = Range("A" & Rows.Count).End(xlUp).Row.Offset(-5, 0)
to:


Code:
End_Row = Range("A" & Rows.Count).End(xlUp).Offset(-5, 0).Row
But personally I wouldn't use a loop, but to each one's own.

Hope that helps.
 
Upvote 0
Hi Schielrn

Thanks for the help. Code now works perfectly

Regards

Howard
 
Upvote 0

Forum statistics

Threads
1,214,625
Messages
6,120,598
Members
448,973
Latest member
ksonnia

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