Alternate between 1 and 0 for each unique row

regresss

Board Regular
Joined
May 20, 2015
Messages
68
Hi, I would like to alternate between colors (or 0s and 1s) for unique row values. That is, if the row above is same, use same value, otherwise use other value. How would you do this?

I have a solution which seems too complex...

<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; white-space: inherit;">=IF(A2=A3, 1, -2)
</code>=IF($A2=$A3, B2, IF($A3=$A4, IF(B2>0,3-B2,B2+3), IF(B2>0,-B2,B2)))
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Do you mean something like this?
Code:
=IF(A2=A3,0,1)
and copying the formula down the rows.

Or is there more to it, something that you aren't telling us.
 
Upvote 0
Here's an example

[TABLE="width: 128"]
<colgroup><col width="64" span="2" style="width:48pt"> </colgroup><tbody>[TR]
[TD="width: 64"]jack[/TD]
[TD="width: 64, align: right"]0[/TD]
[/TR]
[TR]
[TD]jack[/TD]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD]bob[/TD]
[TD="align: right"]1[/TD]
[/TR]
[TR]
[TD]martin[/TD]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD]martin[/TD]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD]martin[/TD]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD]claire[/TD]
[TD="align: right"]1[/TD]
[/TR]
[TR]
[TD]claire[/TD]
[TD="align: right"]1[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
Let's say that your data is in the range A2:B9.
Place zero in cell B2 (the first column B entry).
Then place this formula in B3 and copy down to row B9:
Code:
=IF(A3=A2,B2,MOD(B2+1,2))
 
Upvote 0
Beautiful, thought it is about modulo :-). Cheers.


If anyone wants case-sensitive, try IF(EXACT(A2,A3),B2,MOD(B2+1,2)). But there is probably computational overhead on this so only if needed.
 
Last edited:
Upvote 0
=IF(A3=A2,B2,MOD(B2+1,2))

The above start automatically with 0 as blank cell is considered 0. Would it be possible to start with 1? Sure I could make something like if ROW()-1=1 then..., but this would get validated with each run; I also do not want to place 1 above.

The idea is to return effortlessly something as below.

[TABLE="width: 128"]
<tbody>[TR]
[TD="width: 64"]A[/TD]
[TD="width: 64, align: right"]0[/TD]
[/TR]
[TR]
[TD]A[/TD]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD]A[/TD]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD]B[/TD]
[TD="align: right"]1[/TD]
[/TR]
[TR]
[TD]C[/TD]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD]C[/TD]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD]C[/TD]
[TD="align: right"]0[/TD]
[/TR]
</tbody>[/TABLE]
 
Last edited:
Upvote 0
I am not sure what you mean by:
but this would get validated with each run
but perhaps something like:
Code:
=IF(ROW()=2,1,IF(A3=A2,B2,MOD(B2+1,2)))

Or if you let us know what is in cell B1 (some sort of header, perhaps), we may be able to make use of that, i.e.
Code:
=IF(ISNUMBER(B1)=FALSE,1,IF(A3=A2,B2,MOD(B2+1,2)))
 
Last edited:
Upvote 0
For conditional formatting in groups, of course this helper column doesn't have to be 0/1. Here are 2 more options to consider
Row 1 is manually entered 1 or -1 for the first example and 1 or 0 for the second.

Excel Workbook
ABC
1Data11
2a-10
3a-10
4b11
5b11
6b11
7b11
8c-10
9c-10
10d11
11d11
12e-10
Cond Format Groups
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,279
Members
452,630
Latest member
OdubiYouth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top