Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
Bob Bob is offline
external usenet poster
 
Posts: 972
Default Removing blank rows

I have a 1-column spreadsheet that contains data in column H (with a heading
in cell H1). Some rows in column H contain no data or a single space.
Starting with cell H2, I needed a macro that would examine each cell in
column H and automatically delete a row where no data or only 1 space exists.
The macro would terminate after reaching row 1,000. Someone else in this
forum proposed the following macro:

Set currentCell = Worksheets("Sheet1").Range("H2")
for each cell in range("H2:H1000")
Set nextCell = currentCell.Offset(1, 0)
If Len(currentCell.Value) <= 1 Then
currentCell.EntireRow.Delete
End If
Set currentCell = nextCell
Next

Unfortunately, when I run the macro the first time, SOME rows with cells
whose LEN<=1 still remains! When I run the macro a second time, it then
catches and removes those remaining rows. Does anyone have any idea why I
need to run the aforementioned macro twice for it to truly complete the job?

I would greatly appreciate any help. Thanks.

Bob

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Removing blank rows

Always start on the bottom and go up when you try to delete rows Bob

See
http://www.rondebruin.nl/delete.htm

--
Regards Ron De Bruin
http://www.rondebruin.nl



"Bob" wrote in message ...
I have a 1-column spreadsheet that contains data in column H (with a heading
in cell H1). Some rows in column H contain no data or a single space.
Starting with cell H2, I needed a macro that would examine each cell in
column H and automatically delete a row where no data or only 1 space exists.
The macro would terminate after reaching row 1,000. Someone else in this
forum proposed the following macro:

Set currentCell = Worksheets("Sheet1").Range("H2")
for each cell in range("H2:H1000")
Set nextCell = currentCell.Offset(1, 0)
If Len(currentCell.Value) <= 1 Then
currentCell.EntireRow.Delete
End If
Set currentCell = nextCell
Next

Unfortunately, when I run the macro the first time, SOME rows with cells
whose LEN<=1 still remains! When I run the macro a second time, it then
catches and removes those remaining rows. Does anyone have any idea why I
need to run the aforementioned macro twice for it to truly complete the job?

I would greatly appreciate any help. Thanks.

Bob



  #3   Report Post  
Posted to microsoft.public.excel.programming
Bob Bob is offline
external usenet poster
 
Posts: 972
Default Removing blank rows

Ron,
Thanks for your help! I sincerely appreciate it. Unfortunately, I am a
novice programmer, and after reviewing the info on your site, I was
overwhelmed. To be candid, I was hoping for someone to simply modify the
code below so it would work the first time (rather than having to run it
twice).
Regards, Bob


"Ron de Bruin" wrote:

Always start on the bottom and go up when you try to delete rows Bob

See
http://www.rondebruin.nl/delete.htm

--
Regards Ron De Bruin
http://www.rondebruin.nl



"Bob" wrote in message ...
I have a 1-column spreadsheet that contains data in column H (with a heading
in cell H1). Some rows in column H contain no data or a single space.
Starting with cell H2, I needed a macro that would examine each cell in
column H and automatically delete a row where no data or only 1 space exists.
The macro would terminate after reaching row 1,000. Someone else in this
forum proposed the following macro:

Set currentCell = Worksheets("Sheet1").Range("H2")
for each cell in range("H2:H1000")
Set nextCell = currentCell.Offset(1, 0)
If Len(currentCell.Value) <= 1 Then
currentCell.EntireRow.Delete
End If
Set currentCell = nextCell
Next

Unfortunately, when I run the macro the first time, SOME rows with cells
whose LEN<=1 still remains! When I run the macro a second time, it then
catches and removes those remaining rows. Does anyone have any idea why I
need to run the aforementioned macro twice for it to truly complete the job?

I would greatly appreciate any help. Thanks.

Bob




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 70
Default Removing blank rows

Grüezi Bob

Bob schrieb am 20.06.2006

I have a 1-column spreadsheet that contains data in column H (with a heading
in cell H1). Some rows in column H contain no data or a single space.
Starting with cell H2, I needed a macro that would examine each cell in
column H and automatically delete a row where no data or only 1 space exists.

I would greatly appreciate any help.


Try the following code:

With Range("H:H")
.Replace " ", "", xlWhole
.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End With



Mit freundlichen Grüssen
Thomas Ramel

--
- MVP für Microsoft-Excel -
[Win XP Pro SP-2 / xl2000 SP-3]
Microsoft Excel - Die ExpertenTipps:
(http://tinyurl.com/9ov3l und http://tinyurl.com/cmned)
  #5   Report Post  
Posted to microsoft.public.excel.programming
Bob Bob is offline
external usenet poster
 
Posts: 972
Default Removing blank rows

Thomas,
Very slick! I gave your macro a try and was amazed at how fast it works.
In fact, it even caught cells with 2 blank spaces. Thank you! I sincerely
appreciate all your help.
Regards, Bob

"Thomas Ramel" wrote:

Grüezi Bob

Bob schrieb am 20.06.2006

I have a 1-column spreadsheet that contains data in column H (with a heading
in cell H1). Some rows in column H contain no data or a single space.
Starting with cell H2, I needed a macro that would examine each cell in
column H and automatically delete a row where no data or only 1 space exists.

I would greatly appreciate any help.


Try the following code:

With Range("H:H")
.Replace " ", "", xlWhole
.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End With



Mit freundlichen Grüssen
Thomas Ramel

--
- MVP für Microsoft-Excel -
[Win XP Pro SP-2 / xl2000 SP-3]
Microsoft Excel - Die ExpertenTipps:
(http://tinyurl.com/9ov3l und http://tinyurl.com/cmned)



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Removing blank rows

i had a similar situation and applied the following logic:

With Range("h2:h1000")
'row i am deleting
index = '''''''

..Cells(index + 1, 1).ClearContents

'bubble up the rest of the materials
For last = 1 To 998
If .Cells(last , 1) = "" or .cells(last,1)=" " Then
For r = last To 998
If .Cells(r + 1, 1) < "" Then
.Cells(last + 1, 1) = .Cells(r + 1, 1)
.Cells(r + 1, 1).ClearContents
Exit For
End If
Next r
End If
Next last
End With

i also would be interested if someone else had a more efficient way of
doing this (recusrisve function?)

Bob wrote:
I have a 1-column spreadsheet that contains data in column H (with a heading
in cell H1). Some rows in column H contain no data or a single space.
Starting with cell H2, I needed a macro that would examine each cell in
column H and automatically delete a row where no data or only 1 space exists.
The macro would terminate after reaching row 1,000. Someone else in this
forum proposed the following macro:

Set currentCell = Worksheets("Sheet1").Range("H2")
for each cell in range("H2:H1000")
Set nextCell = currentCell.Offset(1, 0)
If Len(currentCell.Value) <= 1 Then
currentCell.EntireRow.Delete
End If
Set currentCell = nextCell
Next

Unfortunately, when I run the macro the first time, SOME rows with cells
whose LEN<=1 still remains! When I run the macro a second time, it then
catches and removes those remaining rows. Does anyone have any idea why I
need to run the aforementioned macro twice for it to truly complete the job?

I would greatly appreciate any help. Thanks.

Bob


  #7   Report Post  
Posted to microsoft.public.excel.programming
Bob Bob is offline
external usenet poster
 
Posts: 972
Default Removing blank rows

Mark,
Thanks for your help! Check out Thomas Ramel's solution in the thread
above. It is extremely concise. I tried it out on my data and was amazed at
how fast it worked! I'm going to have to study his code closely to
understand how it works.
Regards, Bob

" wrote:

i had a similar situation and applied the following logic:

With Range("h2:h1000")
'row i am deleting
index = '''''''

..Cells(index + 1, 1).ClearContents

'bubble up the rest of the materials
For last = 1 To 998
If .Cells(last , 1) = "" or .cells(last,1)=" " Then
For r = last To 998
If .Cells(r + 1, 1) < "" Then
.Cells(last + 1, 1) = .Cells(r + 1, 1)
.Cells(r + 1, 1).ClearContents
Exit For
End If
Next r
End If
Next last
End With

i also would be interested if someone else had a more efficient way of
doing this (recusrisve function?)

Bob wrote:
I have a 1-column spreadsheet that contains data in column H (with a heading
in cell H1). Some rows in column H contain no data or a single space.
Starting with cell H2, I needed a macro that would examine each cell in
column H and automatically delete a row where no data or only 1 space exists.
The macro would terminate after reaching row 1,000. Someone else in this
forum proposed the following macro:

Set currentCell = Worksheets("Sheet1").Range("H2")
for each cell in range("H2:H1000")
Set nextCell = currentCell.Offset(1, 0)
If Len(currentCell.Value) <= 1 Then
currentCell.EntireRow.Delete
End If
Set currentCell = nextCell
Next

Unfortunately, when I run the macro the first time, SOME rows with cells
whose LEN<=1 still remains! When I run the macro a second time, it then
catches and removes those remaining rows. Does anyone have any idea why I
need to run the aforementioned macro twice for it to truly complete the job?

I would greatly appreciate any help. Thanks.

Bob



  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Removing blank rows

Hi Bob

Be aware that SpecialCells errors when there are no blanks
Use a on error

Also there is a limit

See
http://www.rondebruin.nl/specialcells.htm


--
Regards Ron De Bruin
http://www.rondebruin.nl



"Bob" wrote in message ...
Mark,
Thanks for your help! Check out Thomas Ramel's solution in the thread
above. It is extremely concise. I tried it out on my data and was amazed at
how fast it worked! I'm going to have to study his code closely to
understand how it works.
Regards, Bob

" wrote:

i had a similar situation and applied the following logic:

With Range("h2:h1000")
'row i am deleting
index = '''''''

..Cells(index + 1, 1).ClearContents

'bubble up the rest of the materials
For last = 1 To 998
If .Cells(last , 1) = "" or .cells(last,1)=" " Then
For r = last To 998
If .Cells(r + 1, 1) < "" Then
.Cells(last + 1, 1) = .Cells(r + 1, 1)
.Cells(r + 1, 1).ClearContents
Exit For
End If
Next r
End If
Next last
End With

i also would be interested if someone else had a more efficient way of
doing this (recusrisve function?)

Bob wrote:
I have a 1-column spreadsheet that contains data in column H (with a heading
in cell H1). Some rows in column H contain no data or a single space.
Starting with cell H2, I needed a macro that would examine each cell in
column H and automatically delete a row where no data or only 1 space exists.
The macro would terminate after reaching row 1,000. Someone else in this
forum proposed the following macro:

Set currentCell = Worksheets("Sheet1").Range("H2")
for each cell in range("H2:H1000")
Set nextCell = currentCell.Offset(1, 0)
If Len(currentCell.Value) <= 1 Then
currentCell.EntireRow.Delete
End If
Set currentCell = nextCell
Next

Unfortunately, when I run the macro the first time, SOME rows with cells
whose LEN<=1 still remains! When I run the macro a second time, it then
catches and removes those remaining rows. Does anyone have any idea why I
need to run the aforementioned macro twice for it to truly complete the job?

I would greatly appreciate any help. Thanks.

Bob





  #9   Report Post  
Posted to microsoft.public.excel.programming
Bob Bob is offline
external usenet poster
 
Posts: 972
Default Removing blank rows

Ron,
Thanks for the heads-up. Fortunately, the range of my data never exceeds
more than a 1,000 rows, and it's only 1-column wide.
With respect to adding an OnError check, I can certainly do that, but my
data always has blank cells. So do I really need to add the check? If so,
where in Ramel's code would I do so? Thanks for your help.
Regards, Bob


"Ron de Bruin" wrote:

Hi Bob

Be aware that SpecialCells errors when there are no blanks
Use a on error

Also there is a limit

See
http://www.rondebruin.nl/specialcells.htm


--
Regards Ron De Bruin
http://www.rondebruin.nl



"Bob" wrote in message ...
Mark,
Thanks for your help! Check out Thomas Ramel's solution in the thread
above. It is extremely concise. I tried it out on my data and was amazed at
how fast it worked! I'm going to have to study his code closely to
understand how it works.
Regards, Bob

" wrote:

i had a similar situation and applied the following logic:

With Range("h2:h1000")
'row i am deleting
index = '''''''

..Cells(index + 1, 1).ClearContents

'bubble up the rest of the materials
For last = 1 To 998
If .Cells(last , 1) = "" or .cells(last,1)=" " Then
For r = last To 998
If .Cells(r + 1, 1) < "" Then
.Cells(last + 1, 1) = .Cells(r + 1, 1)
.Cells(r + 1, 1).ClearContents
Exit For
End If
Next r
End If
Next last
End With

i also would be interested if someone else had a more efficient way of
doing this (recusrisve function?)

Bob wrote:
I have a 1-column spreadsheet that contains data in column H (with a heading
in cell H1). Some rows in column H contain no data or a single space.
Starting with cell H2, I needed a macro that would examine each cell in
column H and automatically delete a row where no data or only 1 space exists.
The macro would terminate after reaching row 1,000. Someone else in this
forum proposed the following macro:

Set currentCell = Worksheets("Sheet1").Range("H2")
for each cell in range("H2:H1000")
Set nextCell = currentCell.Offset(1, 0)
If Len(currentCell.Value) <= 1 Then
currentCell.EntireRow.Delete
End If
Set currentCell = nextCell
Next

Unfortunately, when I run the macro the first time, SOME rows with cells
whose LEN<=1 still remains! When I run the macro a second time, it then
catches and removes those remaining rows. Does anyone have any idea why I
need to run the aforementioned macro twice for it to truly complete the job?

I would greatly appreciate any help. Thanks.

Bob





  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Removing blank rows

Hi Bob

With Range("H:H")
.Replace " ", "", xlWhole
On Error Resume Next 'In case there are no blank cells
.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
On Error GoTo 0
End With


--
Regards Ron De Bruin
http://www.rondebruin.nl



"Bob" wrote in message ...
Ron,
Thanks for the heads-up. Fortunately, the range of my data never exceeds
more than a 1,000 rows, and it's only 1-column wide.
With respect to adding an OnError check, I can certainly do that, but my
data always has blank cells. So do I really need to add the check? If so,
where in Ramel's code would I do so? Thanks for your help.
Regards, Bob


"Ron de Bruin" wrote:

Hi Bob

Be aware that SpecialCells errors when there are no blanks
Use a on error

Also there is a limit

See
http://www.rondebruin.nl/specialcells.htm


--
Regards Ron De Bruin
http://www.rondebruin.nl



"Bob" wrote in message ...
Mark,
Thanks for your help! Check out Thomas Ramel's solution in the thread
above. It is extremely concise. I tried it out on my data and was amazed at
how fast it worked! I'm going to have to study his code closely to
understand how it works.
Regards, Bob

" wrote:

i had a similar situation and applied the following logic:

With Range("h2:h1000")
'row i am deleting
index = '''''''

..Cells(index + 1, 1).ClearContents

'bubble up the rest of the materials
For last = 1 To 998
If .Cells(last , 1) = "" or .cells(last,1)=" " Then
For r = last To 998
If .Cells(r + 1, 1) < "" Then
.Cells(last + 1, 1) = .Cells(r + 1, 1)
.Cells(r + 1, 1).ClearContents
Exit For
End If
Next r
End If
Next last
End With

i also would be interested if someone else had a more efficient way of
doing this (recusrisve function?)

Bob wrote:
I have a 1-column spreadsheet that contains data in column H (with a heading
in cell H1). Some rows in column H contain no data or a single space.
Starting with cell H2, I needed a macro that would examine each cell in
column H and automatically delete a row where no data or only 1 space exists.
The macro would terminate after reaching row 1,000. Someone else in this
forum proposed the following macro:

Set currentCell = Worksheets("Sheet1").Range("H2")
for each cell in range("H2:H1000")
Set nextCell = currentCell.Offset(1, 0)
If Len(currentCell.Value) <= 1 Then
currentCell.EntireRow.Delete
End If
Set currentCell = nextCell
Next

Unfortunately, when I run the macro the first time, SOME rows with cells
whose LEN<=1 still remains! When I run the macro a second time, it then
catches and removes those remaining rows. Does anyone have any idea why I
need to run the aforementioned macro twice for it to truly complete the job?

I would greatly appreciate any help. Thanks.

Bob









Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Removing Blank Rows ? Robert11 New Users to Excel 3 November 13th 06 03:07 PM
Removing blank rows Carlton Patterson Excel Programming 2 July 17th 05 12:02 PM
Removing blank rows in a worksheet Louise Excel Worksheet Functions 6 May 26th 05 02:21 PM
Copying and pasting a worksheet to a blank and removing blank rows Bob Reynolds[_3_] Excel Programming 0 June 24th 04 02:55 PM
Removing Blank Rows? andycharger[_35_] Excel Programming 3 June 24th 04 02:09 AM


All times are GMT +1. The time now is 04:58 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"