VBA: Delete blank rows

jimmy2times

Board Regular
Joined
Aug 8, 2014
Messages
69
I work with data on a regular basis that is badly formatted. In the data set I have every alternate row is blank. Each week the amount of data will vary so there could be 500 rows or 10000 rows. Is there a macro i can run that that will start at the first row containing data (typically row 4) and end at the last row of data and delete the blank rows in between? It needs to take into account that the data range will be dynamic rather than static.

It is a pain to this manually and I am inexperienced with VBA.

Thank you
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
I work with data on a regular basis that is badly formatted. In the data set I have every alternate row is blank. Each week the amount of data will vary so there could be 500 rows or 10000 rows. Is there a macro i can run that that will start at the first row containing data (typically row 4) and end at the last row of data and delete the blank rows in between? It needs to take into account that the data range will be dynamic rather than static.

It is a pain to this manually and I am inexperienced with VBA.

Thank you
Hi jimmy2times,

The following code is something that forum moderator and MVP user Peter_SS shared with us a while back. So long as you don't ask me to explain how it works (you can find his own explanation HERE), then I think this will be the fastest and most efficient way I have seen so far for deleting blank rows. The code goes into a standard module and can either be applied to a button or accessed via the ALT+F8 macro window to be run:

Code:
Sub Del_Zero()
  Dim a, b
  Dim nc As Long, i As Long
 
  nc = Cells.Find(What:="*", After:=Cells(1, 1), LookIn:=xlValues, SearchOrder:=xlByColumns, _
                SearchDirection:=xlPrevious, SearchFormat:=False).Column + 1
  a = Range("D1", Range("D" & Rows.Count).End(xlUp)).Value
  ReDim b(1 To UBound(a), 1 To 1)
  For i = 1 To UBound(a)
    If a(i, 1) = 0 Then b(i, 1) = 1
  Next i
  Application.ScreenUpdating = False
  With Range("A1").Resize(UBound(a), nc)
    .Columns(nc).Value = b
    .Sort Key1:=.Columns(nc), Order1:=xlAscending, Header:=xlNo, _
          OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
    On Error Resume Next
    .Columns(nc).SpecialCells(xlConstants).EntireRow.Delete
    On Error GoTo 0
  End With
  Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,906
Members
452,366
Latest member
TePunaBloke

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