Solved

Remove Blank Rows in Excel / CSV file

Posted on 2010-09-10
8
838 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

860 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