Increment Number in a column every time a cell changes color

naylorpsu

New Member
If I have a column where some cells are blue and some are white, I want to start at 1 and increment by 1 only when the color changes in the cell below it. If a cell changes colors again, I would like to increment by 1 again all the way to the bottom of the column.

Could someone point me in the right direction?

Thank you in advance and have a great evening,
Bob

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
If I have a column where some cells are blue and some are white, I want to start at 1 and increment by 1 only when the color changes in the cell below it. If a cell changes colors again, I would like to increment by 1 again all the way to the bottom of the column.
You were a little skimpy on details, so I guessed your column was Column D and that you wanted the count presented to you in a MessageBox...
Code:
``````[table="width: 500"]
[tr]
[td]Sub CountCellColorChanges()
Dim R As Long, Cnt As Long, Col As Range
Set Col = Columns("D")
For R = 1 To ActiveSheet.UsedRange.Rows.Count
If Cells(R, Col.Column).DisplayFormat.Interior.Color <> Cells(R + 1, Col.Column).DisplayFormat.Interior.Color Then Cnt = Cnt + 1
Next
MsgBox Cnt
End Sub[/td]
[/tr]
[/table]``````

Sorry about the lack of detail. So it can be any column...Column D will work. The Column D does not contain any data, just the cell color. I would like the value to be displayed in the cell.

So picture the following:

Cells D1 through D3 cell color are white
Cell D4 through D5 are blue
Cell D6 is white

D1 through D3 would contain 1. Values in D1 through D3 would be 1.
Then the color change to blue on D4 would increment to 2. Values in D4 and D5 would be 2.
Then the color change to white on D6 would increment to 3

Thank you again for your help,
Bob

I was able to tweak the code (hopefully it makes sense) and here is what I was able to make work:

Sub CountCellColorChanges()
Dim R As Long, Cnt As Long, Col As Range, ID As Long
Set Col = Columns("A")
ID = 0
For R = 2 To ActiveSheet.UsedRange.Rows.Count
If Cells(R, Col.Column).DisplayFormat.Interior.Color <> Cells(R - 1, Col.Column).DisplayFormat.Interior.Color _
Then ID = ID + 1

Cells(R, Col.Column).Value = ID
Next
End Sub

Thank you for all your help in pointing me in the right direction. I really appreciate you trying to decipher what little information I provided.

Have a great day,
Bob

Replies
0
Views
336
Replies
20
Views
588
Replies
35
Views
616
Replies
2
Views
354
Replies
4
Views
455

1,203,514
Messages
6,055,840
Members
444,828
Latest member
StaffordStag

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.

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

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