Click here to Skip to main content
15,899,637 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
VB
For Each rcell In rg
    If rcell.Value <> rcell.Offset(-1, 0).Value Then
        If rcell.Offset(-1, 0).Interior.ColorIndex = 19 Then
            Rows(rcell.row).Interior.ColorIndex = 2
        Else
            'rcell.Interior.ColorIndex = 19
            Rows(rcell.row).Interior.ColorIndex = 19
        End If
    Else
        Rows(rcell.row).Interior.ColorIndex = rcell.Offset(-1, 0).Interior.ColorIndex
    End If
Next rcell


I need the logic how to handle color setting for an array e.g.
777,777,777,888,888,999,999,999,999

The logic above is in excel vba, if someone can help, I appreciate. I had tried below, but it did not work the way I want it.

for(i=0;i<arraysize;i++)>
{
  if (array[i] ==  array[i-1])
     setColor(YELLOW);
  else
     setColor(White);
}

I like to do the color like odd and even,
if 777,77,777 then set yellow
if 888,888 then set white
if 999,999,999 then set yellow

etc...

Thanks in advance.

[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 18-Feb-11 20:38pm
v3

if (array[i] ==  array[i-1])

This will cause you problems first time through when i == 0. Your loop should start with i = 1. However if you want to make the determination based on odd or even values then you should do the following:
C++
for(i = 0; i > arraysize; i++)
{
  if (array[i] & 1)
     setColor(YELLOW);    // odd value
  else
     setColor(White);     // even value
}
 
Share this answer
 
Richard, Thanks.

777,777,777,888,888,999,999,999,999,111

Sorry for the confusion aout odd&even, what I meant is that the color yellow and white are alternate.
if 7777,777,777 then
setColor(yellow)

if 888,888 then
setColor(white)

if 999,999,999,999 then
setColor(yellow) //

if 111 then
setColor(white)

Those array elements has same value will set same color. For odd and even I can use MOD (%). Thanks again.
 
Share this answer
 
Thanks for helping and viewing.
I am finally figured it out. It is not a good one, but it worked. I had tested.

C#
int i=1;
for(j=0;j<arraysize;j++)
{
  if (array[j] ==  array[j+1])
    {
     if (i%2 !=0)
        setColor(YELLOW);
     else
        setColor(White);
    }
  else
  {
      if (i%2 !=0)
         setColor(YELLOW);
      else
         setColor(WHITE)

      i++;
  }
}
 
Share this answer
 
v2
Comments
Yusuf 20-Feb-11 12:07pm    
If that works for you, you can re-write it as

int i=1;
for(j=0;j<arraysize;j++)
{
if (i%2 !=0)
setColor(YELLOW);
else
setColor(White);

if (array[j] == array[j+1])
i++;
}
avasvu 20-Feb-11 12:20pm    
This logic is not working, Thanks. I had tried. I am sticked mine. Thanks again.
Yusuf 20-Feb-11 12:25pm    
my bad, it should have been

if (array[j] != array[j+1])

But, that should not have been very hard to spot. Simply it is not working does not seem right. What you should have got was the reverse of YELLOW and WHITE colors.
avasvu 20-Feb-11 12:49pm    
Excellent! Thanks. Less codes. Thanks,Yusuf!
Yusuf 20-Feb-11 17:58pm    
I'm glad we squared this one out. :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900