Solved

Remove Blank Rows in Excel / CSV file

Posted on 2010-09-10
8
831 Views
Last Modified: 2012-05-10
Hi,

I have a few lists that I need to standardise. They are CSV files and have blank rows in them.

How can I remove any blank rows programmatically in Excel?

Thanks,

Ben
0
Comment
Question by:intangiblemedia
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 17

Expert Comment

by:gtgloner
ID: 33648601
Here is some example code of how it might be done. This assumes that your data is in columns A and B:
Sub Macro1()



    Columns("A:B").Select

    Selection.AutoFilter

    Selection.AutoFilter Field:=1, Criteria1:="<>"

    Range("A1:B65536").Select

    Selection.Copy

    Sheets("Sheet2").Select

    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _

        :=False, Transpose:=False

        

End Sub

Open in new window

0
 
LVL 17

Accepted Solution

by:
gtgloner earned 125 total points
ID: 33648629
...hmmm, forgot one crucial line of code, use this instead:
Sub Macro1()



    Columns("A:B").Select

    Selection.AutoFilter

    Selection.AutoFilter Field:=1, Criteria1:="<>"

    Range("A1:B65536").Select

    Selection.Copy

    Sheets("Sheet2").Select

    Range("A1").Select

    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _

        :=False, Transpose:=False

        

End Sub

Open in new window

0
 
LVL 13

Assisted Solution

by:MWGainesJR
MWGainesJR earned 125 total points
ID: 33648747
if your csv has 8000 rows or less, this will do it much more efficiently:

Sub delete_blank_rows()

ActiveSheet.Cells.SpecialCells(xlCellTypeBlanks).Delete

End Sub

Open in new window

0
 
LVL 13

Expert Comment

by:MWGainesJR
ID: 33648782
sorry, my macro is for deleting cells not rows....sorry......disregard it
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 39

Assisted Solution

by:nutsch
nutsch earned 125 total points
ID: 33650290
if all blank rows have the first cell empty, and all non-blank rows have their first cell full, you can use MWGainesJR's solution with a tweak.

THomas

Sub delete_blank_rows()
ActiveSheet.columns("A").SpecialCells(xlCellTypeBlanks).entirerow.Delete
End Sub

Open in new window

0
 
LVL 39

Expert Comment

by:nutsch
ID: 33650305
Warning on gtgloner's solution. Just as mine, it assumes that if the cell in column A is empty, then the whole row is empty.

Let us know if that's not the case and we can adjust the code to fit your needs,

Thomas
0
 
LVL 50

Assisted Solution

by:Dave Brett
Dave Brett earned 125 total points
ID: 33670744
The usual way to delete rows if the entire row is blank is to loop bottom up checking for empty cells using COUNTA

The SpecialCells approach is the way to go if the shortcut is appropriate

Cheers

Dave

'From the Menu, choose Insert-Module.
'Paste the code into the right-hand code window.
'Press Alt + F11 to close the VBE
'Go to Tools & Macro & Macros and double-click DeleteBlankRows
 
Sub DeleteBlankRows()
    Dim rng1 As Range
    With Application
        AppCalc = .Calculation
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
    End With
 
    For r = ActiveSheet.UsedRange.Rows.Count To 1 Step -1
        If Application.CountA(ActiveSheet.UsedRange.Rows(r)) = 0 Then ActiveSheet.UsedRange.Rows(r).Delete
    Next
 
    With Application
        .ScreenUpdating = True
        .Calculation = AppCalc
    End With
 
End Sub

Open in new window

0
 

Author Closing Comment

by:intangiblemedia
ID: 33816939
thanks all
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now