Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 67
Default Why does this happen same code different results

The following code takes a range that consists of cells that are all
from the B column. When the data in the A column is red it makes the
row next to it red(the B column) along with the three rows under.
Example Column A row 20 is red so column B row 20 through 24 would
turn red. It works great everytime. However, I want those colums that
are red to be deleted so I did this little state "If Cells
(i,w).Font.ColorIndex=3 Then Cells(i,w).delete" I have i= 1 to 100 and
w=1 to 100. I thought that that would look in my range and find all of
the data in red and delete it but it doesn't for some reason just
some of the read is still there. So I thought well what if instead of
initially turning those four rows red (the statements below) I just
say .delete instead of rng3.Font.ColorIndex = 3 and the same rows that
were not delete from the prior attempt again are there. So basically I
right the same code and just switch rng3.Font.ColorIndex = 3
to .Delete but I get different outcome. Why is that? Thanks for the
help.



Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range
Dim e As Integer


For e = 1 To 300
Set rng1 = Range(Cells(e, 2), Cells(e + 1, 2))
Set rng2 = Range(Cells(e + 2, 2), Cells(e + 3, 2))
Set rng3 = Union(rng1, rng2)
If Cells(e, 1).Font.ColorIndex = 3 Then rng3.Font.ColorIndex =
3<----'Try to switch to.Delete

Next
End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
Ken Ken is offline
external usenet poster
 
Posts: 207
Default Why does this happen same code different results

Carlos

Try running your loop from bottom to top (e=300 to 1 step -1). When
you are deleting rows, and you start at the top, you can miss some
stuff that you want to delete. For example, if row 4 is red it gets
deleted; but, then what was row 5 becomes row 4. So, your next time
through the loop, if the original row 5 should have been deleted, it
is spared because it became row 4 and you already checked, and
deleted, row 4.

Good luck.

Ken
Norfolk, Va


On Jul 13, 2:43 pm, wrote:
The following code takes a range that consists of cells that are all
from the B column. When the data in the A column is red it makes the
row next to it red(the B column) along with the three rows under.
Example Column A row 20 is red so column B row 20 through 24 would
turn red. It works great everytime. However, I want those colums that
are red to be deleted so I did this little state "If Cells
(i,w).Font.ColorIndex=3 Then Cells(i,w).delete" I have i= 1 to 100 and
w=1 to 100. I thought that that would look in my range and find all of
the data in red and delete it but it doesn't for some reason just
some of the read is still there. So I thought well what if instead of
initially turning those four rows red (the statements below) I just
say .delete instead of rng3.Font.ColorIndex = 3 and the same rows that
were not delete from the prior attempt again are there. So basically I
right the same code and just switch rng3.Font.ColorIndex = 3
to .Delete but I get different outcome. Why is that? Thanks for the
help.

Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range
Dim e As Integer

For e = 1 To 300
Set rng1 = Range(Cells(e, 2), Cells(e + 1, 2))
Set rng2 = Range(Cells(e + 2, 2), Cells(e + 3, 2))
Set rng3 = Union(rng1, rng2)
If Cells(e, 1).Font.ColorIndex = 3 Then rng3.Font.ColorIndex =
3<----'Try to switch to.Delete

Next
End Sub



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 67
Default Why does this happen same code different results

On Jul 13, 2:55 pm, Ken wrote:
Carlos

Try running your loop from bottom to top (e=300 to 1 step -1). When
you are deleting rows, and you start at the top, you can miss some
stuff that you want to delete. For example, if row 4 is red it gets
deleted; but, then what was row 5 becomes row 4. So, your next time
through the loop, if the original row 5 should have been deleted, it
is spared because it became row 4 and you already checked, and
deleted, row 4.

Good luck.

Ken
Norfolk, Va

On Jul 13, 2:43 pm, wrote:



The following code takes a range that consists of cells that are all
from the B column. When the data in the A column is red it makes the
row next to it red(the B column) along with the three rows under.
Example Column A row 20 is red so column B row 20 through 24 would
turn red. It works great everytime. However, I want those colums that
are red to be deleted so I did this little state "If Cells
(i,w).Font.ColorIndex=3 Then Cells(i,w).delete" I have i= 1 to 100 and
w=1 to 100. I thought that that would look in my range and find all of
the data in red and delete it but it doesn't for some reason just
some of the read is still there. So I thought well what if instead of
initially turning those four rows red (the statements below) I just
say .delete instead of rng3.Font.ColorIndex = 3 and the same rows that
were not delete from the prior attempt again are there. So basically I
right the same code and just switch rng3.Font.ColorIndex = 3
to .Delete but I get different outcome. Why is that? Thanks for the
help.


Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range
Dim e As Integer


For e = 1 To 300
Set rng1 = Range(Cells(e, 2), Cells(e + 1, 2))
Set rng2 = Range(Cells(e + 2, 2), Cells(e + 3, 2))
Set rng3 = Union(rng1, rng2)
If Cells(e, 1).Font.ColorIndex = 3 Then rng3.Font.ColorIndex =
3<----'Try to switch to.Delete


Next
End Sub- Hide quoted text -


- Show quoted text -


That worked. I was thinking it might have to do with the fact that I
was deleting a row and it might throw it off but I forgot about the
Step function. Thanks so much it worked

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Why does this happen same code different results

You should be using

For e = 300 To 1 Step -1

because as you delete rows, the rows beneath are moved up, and you end up skipping rows.

HTH,
Bernie
MS Excel MVP


wrote in message
oups.com...
The following code takes a range that consists of cells that are all
from the B column. When the data in the A column is red it makes the
row next to it red(the B column) along with the three rows under.
Example Column A row 20 is red so column B row 20 through 24 would
turn red. It works great everytime. However, I want those colums that
are red to be deleted so I did this little state "If Cells
(i,w).Font.ColorIndex=3 Then Cells(i,w).delete" I have i= 1 to 100 and
w=1 to 100. I thought that that would look in my range and find all of
the data in red and delete it but it doesn't for some reason just
some of the read is still there. So I thought well what if instead of
initially turning those four rows red (the statements below) I just
say .delete instead of rng3.Font.ColorIndex = 3 and the same rows that
were not delete from the prior attempt again are there. So basically I
right the same code and just switch rng3.Font.ColorIndex = 3
to .Delete but I get different outcome. Why is that? Thanks for the
help.



Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range
Dim e As Integer


For e = 1 To 300
Set rng1 = Range(Cells(e, 2), Cells(e + 1, 2))
Set rng2 = Range(Cells(e + 2, 2), Cells(e + 3, 2))
Set rng3 = Union(rng1, rng2)
If Cells(e, 1).Font.ColorIndex = 3 Then rng3.Font.ColorIndex =
3<----'Try to switch to.Delete

Next
End Sub



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default Why does this happen same code different results

Try this

If Cells(e, 1).Font.ColorIndex = 3 Then
rng3.Select
Selection.Delete
End If

Mike

" wrote:

The following code takes a range that consists of cells that are all
from the B column. When the data in the A column is red it makes the
row next to it red(the B column) along with the three rows under.
Example Column A row 20 is red so column B row 20 through 24 would
turn red. It works great everytime. However, I want those colums that
are red to be deleted so I did this little state "If Cells
(i,w).Font.ColorIndex=3 Then Cells(i,w).delete" I have i= 1 to 100 and
w=1 to 100. I thought that that would look in my range and find all of
the data in red and delete it but it doesn't for some reason just
some of the read is still there. So I thought well what if instead of
initially turning those four rows red (the statements below) I just
say .delete instead of rng3.Font.ColorIndex = 3 and the same rows that
were not delete from the prior attempt again are there. So basically I
right the same code and just switch rng3.Font.ColorIndex = 3
to .Delete but I get different outcome. Why is that? Thanks for the
help.



Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range
Dim e As Integer


For e = 1 To 300
Set rng1 = Range(Cells(e, 2), Cells(e + 1, 2))
Set rng2 = Range(Cells(e + 2, 2), Cells(e + 3, 2))
Set rng3 = Union(rng1, rng2)
If Cells(e, 1).Font.ColorIndex = 3 Then rng3.Font.ColorIndex =
3<----'Try to switch to.Delete

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
Wow... Anyone ever seen this happen? Dan R. Excel Discussion (Misc queries) 1 March 16th 07 07:34 PM
Why is that happen ? 0xC00D11CD Excel Worksheet Functions 1 May 22nd 06 01:14 PM
Same code - different results scott_hanebutt Excel Programming 0 January 3rd 06 07:59 PM
why would this happen? JENNYC Excel Discussion (Misc queries) 3 December 12th 05 05:56 PM
Sorry for the attachment. new and just trying to help. It will not happen again. Gary[_18_] Excel Programming 1 July 3rd 04 01:46 AM


All times are GMT +1. The time now is 09:45 AM.

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"