Solved

macro to delete blank rows in excel

Posted on 2011-03-15
9
204 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
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.

 

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

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;…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
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…

863 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

27 Experts available now in Live!

Get 1:1 Help Now