Solved

macro to delete blank rows in excel

Posted on 2011-03-15
9
205 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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
 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

785 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