Solved

macro to delete blank rows in excel

Posted on 2011-03-15
9
206 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
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…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

808 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