Solved

macro to delete blank rows in excel

Posted on 2011-03-15
9
203 Views
Last Modified: 2012-05-11
I'm using Excel 2007.   I have a worksheet with 250,000 records.   There are blank rows mixed amongst the data.  I need to keep the data in the exact same order it is in right now (no  sorting) but delete all blank rows.  A blank row is identifiable if a cell in column "A" is blank.  In other words, check each cell in column "A". If it is blank, then delete the entire row.  How come this macro I'm trying to use to accomplish this isn't working?

 Sub DeleteRows()
Application.ScreenUpdating = False

Dim i As Long
i = 1
Do Until i > Cells(250000, "a").End(xlUp).Row
If Cells(i, "a").Value = "" Then
   Rows(i).Delete
Else
   i = i + 1
End If
Loop
MsgBox "Done"
Application.ScreenUpdating = True
End Sub
0
Comment
Question by:dbfromnewjersey
  • 5
  • 3
9 Comments
 
LVL 24

Expert Comment

by:StephenJR
ID: 35140453
Try this:

Columns(1).SpecialCells(xlcelltypeblanks).entirerow.delete

Open in new window

0
 

Author Comment

by:dbfromnewjersey
ID: 35140563
Actually I just checked my original macro once again as well as your line of code and they both work to remove blank rows when I insert blank rows.  So what must be going on is that the existing "blank" cells/rows are not actually blank. Must be hidden characters or something.  Any thoughts on how to deal with these?  For cells in column "A" that appear to be blank, I want to delete the entire row. I ran a TRIM on the worksheet before doing anything but I guess that didn't properly prepare the file.  
0
 
LVL 24

Assisted Solution

by:StephenJR
StephenJR earned 100 total points
ID: 35140586
Try this. You have to go backwards when deleting rows:
Sub DeleteRows()

Application.ScreenUpdating = False

Dim i As Long

For i = Range("A" & Rows.Count).End(xlUp).Row To 1 Step -1
    If Cells(i, "a").Value = "" Then
        Rows(i).Delete
    End If
Next i

MsgBox "Done"
Application.ScreenUpdating = True

End Sub

Open in new window

0
 

Author Comment

by:dbfromnewjersey
ID: 35140727
Well, before I run that, let just follow up on what I wrote:

I ran the following formula down an empty column to try to check what's in column "A". It seems that every time a cell in column "A" appears blank, "32" is being placed in my test column. Not sure if the 32 is just the length of the hidden data or it has some other meaning.   Since there are 250,000 records, I can't say for certain that "32" doesn't appear in "nonblank" cells in column "A". but from the 100 or so rows I checked, it seems only the "blank" cells in column "A" have a value of "32" according to the formula.  

=CODE(MID($A14,COLUMN(A14),1))
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:dbfromnewjersey
ID: 35140841
That 32 is the Ascii value for a space.  Because for example, another cell that starts with "P" has the value 80 in it (the Ascii value for P).  Why the heck is the macro not seeing the cell as blank?  There must be hidden characters in some other position within the cells.  By the way, I ran your macro that goes from the bottom up and now luck. This problem definitely has to do with hidden characters. I wish I knew how to get rid of them.  Could you show me how to modify your macro to check just the first character in each cell in coumn A?  Maybe that would work. Every cell that does not appear blank does have a character in first position. That much I know.
0
 

Author Comment

by:dbfromnewjersey
ID: 35140856
typo above. I meant no luck, not now luck.
0
 
LVL 24

Expert Comment

by:StephenJR
ID: 35141500
Post a workbook.
0
 
LVL 8

Accepted Solution

by:
wchh earned 150 total points
ID: 35143276
Try to Trim the value
Sub DeleteRows()
Application.ScreenUpdating = False

Dim i As Long
i = 1
Do Until i > Cells(250000, "a").End(xlUp).Row
If Trim(Cells(i, "a").Value) = "" Then 'Trim the value
   Rows(i).Delete
Else
   i = i + 1
End If
Loop
MsgBox "Done"
Application.ScreenUpdating = True
End Sub

Open in new window

0
 

Author Comment

by:dbfromnewjersey
ID: 35158492
Looks like that worked. Thanks a million.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

707 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

11 Experts available now in Live!

Get 1:1 Help Now