Solved

Remove Blank Rows in Excel / CSV file

Posted on 2010-09-10
8
833 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

896 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

14 Experts available now in Live!

Get 1:1 Help Now