Solved

Remove Blank Rows in Excel / CSV file

Posted on 2010-09-10
8
835 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

810 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