Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default macro to clear rows

Hi, I have a most likely easy question that is absolutely
stumping me.

Let's say I have two columns of data. Whenever the data
in columnA is zero, I want to delete (clear contents) that
entire row of data. Thus, if cell A40 was zero, B40 would
be deleted as well.

I don't even care of the rest of the data is shifted up
with a delete. My problem is that when I run the macro,
it misses a bunch of the zeros that it was supposed to
detect. Any help?

Here's what I'm working with:



Sub delete()

For rwIndex = 1 To 200
With ActiveSheet.Cells(rwIndex, 1)
If ((.Value) = 0) Then
Rows(rwIndex).delete
End If
End With
Next rwIndex

End Sub



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 103
Default macro to clear rows

you need to reverse the loop. a for-next your way 'skips' a row when
deleting
think of it, let us suppose you test A400 which is zero. delete row 400, so
what was 401 becomes 400 and the 'next' part of the loop increments the
counter to 401. thus you simply misses what was 401.

Sub delete()
With ActiveSheet
For rwIndex = 200 to 1 STEP -1
If .Cells(rwIndex, 1).Value = 0 Then
Rows(rwIndex).delete
End If
Next rwIndex
End With
End Sub

Note that I moved the WITH statement outside the loo too...
--
Patrick Molloy
Microsoft Excel MVP
----------------------------------
"Peter H" wrote in message
...
Hi, I have a most likely easy question that is absolutely
stumping me.

Let's say I have two columns of data. Whenever the data
in columnA is zero, I want to delete (clear contents) that
entire row of data. Thus, if cell A40 was zero, B40 would
be deleted as well.

I don't even care of the rest of the data is shifted up
with a delete. My problem is that when I run the macro,
it misses a bunch of the zeros that it was supposed to
detect. Any help?

Here's what I'm working with:



Sub delete()

For rwIndex = 1 To 200
With ActiveSheet.Cells(rwIndex, 1)
If ((.Value) = 0) Then
Rows(rwIndex).delete
End If
End With
Next rwIndex

End Sub





  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 280
Default macro to clear rows

Try using For rwIndex = 200 To 1 Step -1

"Peter H" wrote in message
...
Hi, I have a most likely easy question that is absolutely
stumping me.

Let's say I have two columns of data. Whenever the data
in columnA is zero, I want to delete (clear contents) that
entire row of data. Thus, if cell A40 was zero, B40 would
be deleted as well.

I don't even care of the rest of the data is shifted up
with a delete. My problem is that when I run the macro,
it misses a bunch of the zeros that it was supposed to
detect. Any help?

Here's what I'm working with:



Sub delete()

For rwIndex = 1 To 200
With ActiveSheet.Cells(rwIndex, 1)
If ((.Value) = 0) Then
Rows(rwIndex).delete
End If
End With
Next rwIndex

End Sub





  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 280
Default macro to clear rows

Sub DeleteZeroAndBlankRows()

With Range("A1:A200")
.Replace _
What:="0", _
Replacement:="", _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False

.SpecialCells(xlBlanks).EntireRow.Delete xlUp
End With

End Sub

Bob Kilmer

"Peter H" wrote in message
...
Hi, I have a most likely easy question that is absolutely
stumping me.

Let's say I have two columns of data. Whenever the data
in columnA is zero, I want to delete (clear contents) that
entire row of data. Thus, if cell A40 was zero, B40 would
be deleted as well.

I don't even care of the rest of the data is shifted up
with a delete. My problem is that when I run the macro,
it misses a bunch of the zeros that it was supposed to
detect. Any help?

Here's what I'm working with:



Sub delete()

For rwIndex = 1 To 200
With ActiveSheet.Cells(rwIndex, 1)
If ((.Value) = 0) Then
Rows(rwIndex).delete
End If
End With
Next rwIndex

End Sub





  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 55
Default macro to clear rows

I've run into this before. The reason it's missing items is because
when it deletes the row, the row numbers shift up and your counter,
rowIndex, misses one. If you want to delete, use a Do/While loop. If
clearing is okay, change .delete to .clear.

Sub delete()

rwIndex = 1
Do while rwIndex <= 200
With ActiveSheet.Cells(rwIndex, 1)
If ((.Value) = 0) Then
Rows(rwIndex).delete
Else
rwIndex = rwIndex + 1
End If
End With
Loop

End Sub

Sub delete()

For rwIndex = 1 To 200
With ActiveSheet.Cells(rwIndex, 1)
If ((.Value) = 0) Then
Rows(rwIndex).clear
End If
End With
Next rwIndex

End Sub

Let me know if you have questions.

Mark

---
Mark Bigelow
mjbigelow at hotmail dot com
http://hm.imperialoiltx.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default macro to clear rows

Thank you all very, very, very much!
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 66
Default macro to clear rows

I dont know if the cells in those rows has to meet a criteria if so then:

dim cell as range

for each cell in range ("your range here")

if cell.value = "whatever" then

cell.entirerow.clearcontents

end if
next cell


if you just want to clear rows then

sub clear ()
range("your ranger here").clearcontents


end sub















"Peter" wrote in message
...
Thank you all very, very, very much!



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
Macro to clear contents and put an X bevchapman Excel Discussion (Misc queries) 3 March 17th 09 07:03 PM
clear cells macro Dorothy Excel Discussion (Misc queries) 5 April 7th 08 12:46 AM
macro to clear cells press313 Excel Discussion (Misc queries) 0 May 24th 06 02:31 AM
Clear shhet with macro Micos3 Excel Discussion (Misc queries) 2 February 20th 06 05:50 PM
Clear Contents Macro SJC Excel Worksheet Functions 3 October 27th 05 07:26 PM


All times are GMT +1. The time now is 06:12 PM.

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

About Us

"It's about Microsoft Excel"