If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Excel > Delete the entire row is the cell in column A is blank

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-14-04, 05:07
davidkoh79 davidkoh79 is offline
Registered User
 
Join Date: Feb 2004
Posts: 21
Delete the entire row is the cell in column A is blank

Hi there

I have tried various methods but still can't come up with the solution.I need to write a code which finds blank cells in Column A of an Excel worksheet. And when there is a blank cell in column A, the entire row will be deleted and the rows below it will shift up to occupy that row.The number of rows in column A is not fixed and it will vary from one worksheet to another worksheet.Pls help.Thanks

David
Reply With Quote
  #2 (permalink)  
Old 05-14-04, 14:06
SR22Mike SR22Mike is offline
Registered User
 
Join Date: Mar 2004
Location: Minnesota, USA, Earth
Posts: 65
You could try this...

You could try this...(off the top of my head)


Sub DeleteBlankRows()

Range("A1").select
Do
If activecell = "" Then
activecell.entirerow.delete shift:=xlUp
activecell.offset(-1,0).select
End If
activecell.offset(1,0).select
Loop until....(insert some test for the end of the file).

End Sub
Reply With Quote
  #3 (permalink)  
Old 05-16-04, 09:52
davidkoh79 davidkoh79 is offline
Registered User
 
Join Date: Feb 2004
Posts: 21
Unsure how to determine last row

Hi SR22Mike

The problem I had was to determine the last row in column A as this changes from one worksheet to another.So I am not sure how to to do the loop command.
Reply With Quote
  #4 (permalink)  
Old 05-16-04, 10:15
actuary actuary is offline
Registered User
 
Join Date: Mar 2004
Location: Fort Worth, Texas, USA
Posts: 68
Sub DeleteRowsWithBlanks()
Dim lastrow As Long
lastrow = Range("A:A").SpecialCells(xlLastCell).Row
Range(Cells(1, 1), Cells(lastrow, 1)).SpecialCells(xlCellTypeBlanks).EntireRow.Delet e
End Sub

'This is a good example of the dreadful performance of VBA loops.
'To test, put both of the following into a new workbook, then run each.

Sub DeleteRowsWithBlanks()
Dim lastrow As Long
ActiveWorkbook.Sheets.Add
Range("A1").Value = "foo"
Range("A5000").Value = "bar"
Start = Now()
lastrow = Range("A:A").SpecialCells(xlLastCell).Row
Range(Cells(1, 1), Cells(lastrow, 1)).SpecialCells(xlCellTypeBlanks).EntireRow.Delet e
Cells(1, 2).Value = Now() - Start
Cells(1, 2).NumberFormat = "ss.sss ""seconds"""
End Sub


Sub DeleteBlankRows()
Dim lastrow As Long
Dim i As Long
ActiveWorkbook.Sheets.Add
Range("A1").Value = "foo"
Range("A5000").Value = "bar"
Start = Now()
lastrow = Range("A:A").SpecialCells(xlLastCell).Row
Range("A1").Select
i = 1
Do While i < lastrow
If ActiveCell = "" Then
ActiveCell.EntireRow.Delete shift:=xlUp
ActiveCell.Offset(-1, 0).Select
End If
ActiveCell.Offset(1, 0).Select
i = i + 1
Loop
Cells(1, 2).Value = Now() - Start
Cells(1, 2).NumberFormat = "ss.sss ""seconds"""
End Sub

Last edited by actuary; 05-16-04 at 11:07.
Reply With Quote
  #5 (permalink)  
Old 05-17-04, 05:39
davidkoh79 davidkoh79 is offline
Registered User
 
Join Date: Feb 2004
Posts: 21
Tried a code but too slow

Hi actuary

I have a worksheet with about 30,000 rows and I tried this code :
Sub DeleteBlankRows2()
Dim lastrow As Long
Dim i As Long
lastrow = Range("A:A").SpecialCells(xlLastCell).Row
Range("A1").Select
i = 1
Do While i < lastrow
If ActiveCell = "" Then
ActiveCell.EntireRow.Delete shift:=xlUp
ActiveCell.Offset(-1, 0).Select
End If
ActiveCell.Offset(1, 0).Select
i = i + 1
Loop
End Sub

It took quite a while to do the code. I tried the code that you have given meub DeleteRowsWithBlanks()
Dim lastrow As Long
lastrow = Range("A:A").SpecialCells(xlLastCell).Row
Range(Cells(1, 1), Cells(lastrow, 1)).SpecialCells(xlCellTypeBlanks).EntireRow.Delet e
End Sub

But the whole worksheet turn out blank. Is there a problem with it or is there a faster way other than the first code? Thanks
Reply With Quote
  #6 (permalink)  
Old 05-17-04, 08:56
actuary actuary is offline
Registered User
 
Join Date: Mar 2004
Location: Fort Worth, Texas, USA
Posts: 68
My code is just a VBA version of what can be done with Excel's native functionality. Try doing it manually on the original worksheet: Use your mouse to select all the cells in column A all the way to the bottom of your data. Then choose Edit, Go To..., Special..., Blanks. Then choose Edit, Delete..., Entire row.

If the above doesn't work correctly, then it's a problem with the worksheet, not the code. You'll need to zip it and post it here.
Reply With Quote
  #7 (permalink)  
Old 05-17-04, 09:39
shades shades is offline
Registered User
 
Join Date: Oct 2003
Posts: 1,091
Here is a shrotened version that allows you to choose the column upon which to base the deletion.
Code:
Sub DeleteEmptyRowsMain()
'from Mr. Excel.com which allows user to
'choose the column by selecting it.
Dim myColm As Range
    Set myColm = Application.InputBox("Choose column(s) to clear", Type:=8)
    On Error Resume Next
    myColm.SpecialCells(xlCellTypeBlanks).EntireRow.Delete

End Sub
__________________
old, slow, and confused
but at least I'm inconsistent!

Rich
(retired Excel 2003 user, 3/28/2008)

How to ask a question on forums
Reply With Quote
  #8 (permalink)  
Old 05-17-04, 10:02
davidkoh79 davidkoh79 is offline
Registered User
 
Join Date: Feb 2004
Posts: 21
Set Column as Column A

Hi shades

Thanks for the code.It works perfectly.But if I want to fix the column as the first column(eg:Column A),what value do I set myColm as?Thanks
Reply With Quote
  #9 (permalink)  
Old 05-17-04, 10:07
actuary actuary is offline
Registered User
 
Join Date: Mar 2004
Location: Fort Worth, Texas, USA
Posts: 68
If you want to operate on the whole column anyway, just use:

Code:
Sub DeleteRowsWithBlanks()
Range("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub
'This seems to be just as fast as selecting a limited number of rows.

Last edited by actuary; 05-17-04 at 10:12.
Reply With Quote
  #10 (permalink)  
Old 05-17-04, 10:59
davidkoh79 davidkoh79 is offline
Registered User
 
Join Date: Feb 2004
Posts: 21
Puzzled

Hi there

I tried the code already but this time with 2 different files. One file that has 10 rows and the code works perfectly.Another worksheet with 30,000 rows but the result with the one with the 30,000 rows is a blank worksheet. Is there a limitation or something like that? Thanks
Reply With Quote
  #11 (permalink)  
Old 05-17-04, 11:52
actuary actuary is offline
Registered User
 
Join Date: Mar 2004
Location: Fort Worth, Texas, USA
Posts: 68
I don't know, but if you'll post the spreadsheet we can find out.

Have you tried it manually like I asked? What happened?

Are you absolutely sure the macro produces a blank sheet on the 30,000 row version? In other words, did you press CTRL+Home to go to the top of the sheet and still you saw no rows?

Last edited by actuary; 05-17-04 at 15:21.
Reply With Quote
  #12 (permalink)  
Old 05-17-04, 14:27
shades shades is offline
Registered User
 
Join Date: Oct 2003
Posts: 1,091
Quote:
Originally Posted by davidkoh79
Hi shades

Thanks for the code.It works perfectly.But if I want to fix the column as the first column(eg:Column A),what value do I set myColm as?Thanks
The InputBox allows you to select the Column. So move the mouse over Column A heading and click.
__________________
old, slow, and confused
but at least I'm inconsistent!

Rich
(retired Excel 2003 user, 3/28/2008)

How to ask a question on forums
Reply With Quote
  #13 (permalink)  
Old 05-17-04, 21:06
davidkoh79 davidkoh79 is offline
Registered User
 
Join Date: Feb 2004
Posts: 21
Hi actuary

I tried the code on the 30,000 row worksheet and it turned out blank. My 30,000 row worksheet has data from columns A to Y.And it is filled also from row 1 to row 30,000. But in column A each alternate row is blank .
Reply With Quote
  #14 (permalink)  
Old 05-17-04, 21:18
actuary actuary is offline
Registered User
 
Join Date: Mar 2004
Location: Fort Worth, Texas, USA
Posts: 68
I give up. Post the spreadsheet or don't post at all.
Reply With Quote
  #15 (permalink)  
Old 05-18-04, 21:03
davidkoh79 davidkoh79 is offline
Registered User
 
Join Date: Feb 2004
Posts: 21
Sorry

Hi actuary

Sorry. I have uploaded the file. Pls take a look. Thanks
Attached Files
File Type: zip scan.zip (191.8 KB, 105 views)
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On