Solved

Remove Blank Rows in Excel / CSV file

Posted on 2010-09-10
8
843 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 13

Expert Comment

by:MWGainesJR
ID: 33648782
sorry, my macro is for deleting cells not rows....sorry......disregard it
0
 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

738 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