Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

macro to delete blank rows in excel

Posted on 2011-03-15
9
Medium Priority
?
211 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 400 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 600 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
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 …

671 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