Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
JJ JJ is offline
external usenet poster
 
Posts: 122
Default selecting and deleting a row based on a cell value

What code do I have to use to read through a column of cells and if a cell
has a certain value, it will delete the entire row. I have this code, but it
keeps moving down a space after it deletes the row. For instance, I want to
delete an entire row if the cell value is "2", but if there are numerous "2"s
in a row, it always skips everyother one. Ex: 1,1,2,2,2,2 the result will
show remaining cells 1,1,2,2. Here is what I have so far:
Sub (rowdelete)
For Each rw In Worksheets("sheet1").Cells(1, 1).CurrentRegion.Rows
this = rw.Cells(1, 1).Value
If this = last Then rw.delete
last = this
Next
End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default selecting and deleting a row based on a cell value

Hi JJ

You loop must start on the end of your data and go up

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

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


"JJ" wrote in message ...
What code do I have to use to read through a column of cells and if a cell
has a certain value, it will delete the entire row. I have this code, but it
keeps moving down a space after it deletes the row. For instance, I want to
delete an entire row if the cell value is "2", but if there are numerous "2"s
in a row, it always skips everyother one. Ex: 1,1,2,2,2,2 the result will
show remaining cells 1,1,2,2. Here is what I have so far:
Sub (rowdelete)
For Each rw In Worksheets("sheet1").Cells(1, 1).CurrentRegion.Rows
this = rw.Cells(1, 1).Value
If this = last Then rw.delete
last = this
Next
End Sub



  #4   Report Post  
Posted to microsoft.public.excel.programming
No Name
 
Posts: n/a
Default selecting and deleting a row based on a cell value

Sub macdelrow()
Dim t As Range
Dim n As Range
Set t = Range("a1")
Do While Not IsEmpty(t)
Set n = t.Offset(1, 0)
If t.Value = 2 Then
t.EntireRow.Delete shift:=xlUp
End If
Set t = n
t.Select
Loop
End Sub

-----Original Message-----
What code do I have to use to read through a column of

cells and if a cell
has a certain value, it will delete the entire row. I

have this code, but it
keeps moving down a space after it deletes the row. For

instance, I want to
delete an entire row if the cell value is "2", but if

there are numerous "2"s
in a row, it always skips everyother one. Ex: 1,1,2,2,2,2

the result will
show remaining cells 1,1,2,2. Here is what I have so far:
Sub (rowdelete)
For Each rw In Worksheets("sheet1").Cells(1,

1).CurrentRegion.Rows
this = rw.Cells(1, 1).Value
If this = last Then rw.delete
last = this
Next
End Sub
.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 694
Default selecting and deleting a row based on a cell value

Hi,

Not sure i get it but maybe you could first use a range object to keep track
of the rows to delete within the loop then delete them all at once after the
loop. This way, you don't have to deal with adjusting the code for deleted
rows every time.
Something like:

Dim RgRows as Range
'...
For Each rw In Worksheets("sheet1").Cells(1, 1).CurrentRegion.Rows
this = rw.Cells(1, 1).Value
If this = last Then
if RgRows Is Nothing then '<-----------
set rgRows= rw
else
set rgrows = application.intersect( rgrows, rw)
end if
end if
last = this
Next
rgRows.EntireRow.Delete '<--------------

Regards,
Sebastien

"JJ" wrote:

What code do I have to use to read through a column of cells and if a cell
has a certain value, it will delete the entire row. I have this code, but it
keeps moving down a space after it deletes the row. For instance, I want to
delete an entire row if the cell value is "2", but if there are numerous "2"s
in a row, it always skips everyother one. Ex: 1,1,2,2,2,2 the result will
show remaining cells 1,1,2,2. Here is what I have so far:
Sub (rowdelete)
For Each rw In Worksheets("sheet1").Cells(1, 1).CurrentRegion.Rows
this = rw.Cells(1, 1).Value
If this = last Then rw.delete
last = this
Next
End Sub

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
Selecting a cell entry based on cell validation selection Brutalius Excel Worksheet Functions 2 December 17th 08 03:44 AM
Selecting A Column based on Cell Value newguy Excel Discussion (Misc queries) 0 December 14th 06 09:39 PM
Selecting rows based on cell entries m.cain Excel Discussion (Misc queries) 2 March 24th 06 10:46 AM
Selecting a filepath based on cell contents ceemo Excel Discussion (Misc queries) 1 June 25th 05 01:40 PM
Deleting Rows based on text in cell & formatting cell based on text in column beside it Steve Excel Programming 4 February 26th 04 03:31 PM


All times are GMT +1. The time now is 03:40 AM.

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"