Highlighting rows in a column when number changes (numbers are in consecutive order)

mstaralynn

New Member
Joined
Jan 25, 2023
Messages
5
Office Version
  1. 365
Platform
  1. MacOS
Hello - I have been trying to find a solution to this and have so far can up empty - but it seems like there HAS to be a way. I have a column with numbers in consecutive order - there could be 5 of one number, 3 of the next, 7 of the one after that - so I need to copy/paste info that correlates with the number. Problem is sometimes the numbers are similar and I miss the change and copy the wrong info. If I could have the rows colored, and change each Tim the number changes - I could visually see how far down I need to copy the info. The colors do not matter - its really just a visual guide. I tried the Conditional Formatting, Color Scales - and that doesn't work because they are in consecutive order. Also - find duplicates doesn't work either. Please see in the pic how I want the row to look - it could be the same 2 colors alternating at each number change - color doesn't matter. This is something I do very often and any way to make it easier is nice. THANK YOU!!!
 

Attachments

  • Screen Shot 2023-01-25 at 1.44.45 PM.png
    Screen Shot 2023-01-25 at 1.44.45 PM.png
    169.5 KB · Views: 25

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
If you don't mind a macro this could be a solution. The macro tests numbers in column D and changes background color (light colors) at every change of numbers:
VBA Code:
Option Explicit
Sub Color_Cells()
    Dim rw As Long
    Dim rndColor As Integer
    For rw = 2 To Range("D" & Rows.Count).End(xlUp).Row
        If (Cells(rw, "D") <> Cells(rw - 1, "D")) Then rndColor = Application.RandBetween(33, 45)
        With Cells(rw, "D").Interior
            .ColorIndex = rndColor
        End With
    Next rw
End Sub
 
Upvote 0
Hi mstaralynn,

Welcome to MrExcel!!

Here's my attempt that uses light green and yellow to highlight each block in Col. A (run while on the sheet with the numbers):

VBA Code:
Option Explicit
Sub Macro1()

    Dim objCell As Object
    Dim i As Long, j As Long
    Dim strRGB As String
   
    strRGB = "200,255,200" 'First fill colour (light green)
    j = Cells(Rows.Count, "A").End(xlUp).Row
   
    Application.ScreenUpdating = False
   
    For i = 2 To j
        Set objCell = Range("A" & i)
        If i = 2 Then 'First record
            objCell.Interior.Color = RGB(Split(strRGB, ",")(0), Split(strRGB, ",")(1), Split(strRGB, ",")(2))
        Else
            If Range("A" & i) <> Range("A" & i - 1) Then
                strRGB = IIf(strRGB = "200,255,200", "255,255,155", "200,255,200") 'Switch between light green and light yellow
            End If
            objCell.Interior.Color = RGB(Split(strRGB, ",")(0), Split(strRGB, ",")(1), Split(strRGB, ",")(2))
        End If
    Next i
   
    Application.ScreenUpdating = True

End Sub

Note though that code could actually copy the data for you so there's probably no need to do this.

Regards,

Robert
 
Upvote 0
Thank you! I have never ran a macro before - but will give it a shot!
 
Upvote 0
Conditional formatting like this?

23 01 26.xlsm
D
1Number
25
35
45
55
65
73
83
97
107
117
127
137
146
1545
1645
Bands
Cells with Conditional Formatting
CellConditionCell FormatStop If True
D2:D16Expression=ISEVEN(COUNT(UNIQUE(D$2:D2)))textNO
D2:D16Expression=ISODD(COUNT(UNIQUE(D$2:D2)))textNO
 
Upvote 0
Looking again I notice that your values are left-aligned so may not be numerical. This one should work either way.

23 01 26.xlsm
D
1Number
2a5
3a5
4a5
5a5
6a5
7b3
8b3
97
107
117
127
137
14d6
15e45
16e45
Bands (2)
Cells with Conditional Formatting
CellConditionCell FormatStop If True
D2:D16Expression=ISEVEN(ROWS(UNIQUE(D$2:D2)))textNO
D2:D16Expression=ISODD(ROWS(UNIQUE(D$2:D2)))textNO



Note though that both of my suggestions assume that all the same values are grouped together in the column. It would not work if values reoccur further down the column like "a5" in this sample.

23 01 26.xlsm
D
1Number
2a5
3a5
4a5
5a5
6a5
7b3
8b3
97
107
117
127
137
14a5
15e45
16e45
Bands (3)
 
Upvote 0
Yes! This looks correct. The same values are grouped together. They are numerical, but I also usually have the same values in a different column just as text. I hope this isn’t a dumb question - but is this a formula I can use?
 
Upvote 0
You should be able to ... but the easiest way to answer that for sure would be to try it. ;)
This worked almost - I was confused & then realized it was supposed to go in the conditional formatting formula place - OK - it worked BUT - the last number of a group is staying the same as the next group. Here is a pic & I think a mini-sheet?

Numbers.xlsx
AB
1Number
210969237
310969237
410969237
510969526
610969526
710969526
810969526
910969600
1010969600
1110969600
1210969652
1310969652
1410969652
1510969858
1610969858
1710969858
1810969866
1910969866
2010969866
2110969866
2210969907
2310969907
2410969917
2510969941
2610969941
2710969941
2810969941
2910969941
3010969941
3110969941
3210969941
3310969941
3410970077
3510970077
3610970078
3710970078
3810970078
3910970078
4010970318
4110970318
4210970383
4310970383
4410970385
4510970385
4610970385
4710970385
4810970481
4910970518
5010970518
51
Sheet1
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A:AExpression=ISODD(ROWS(UNIQUE(A$2:A2)))textNO
A:AExpression=ISEVEN(ROWS(UNIQUE(A$2:A2)))textNO
 

Attachments

  • Screen Shot 2023-01-26 at 12.35.19 PM.png
    Screen Shot 2023-01-26 at 12.35.19 PM.png
    150.7 KB · Views: 9
Upvote 0
A couple of issues
  1. You have applied Conditional Formatting to an entire column. How much data do you actually have? Since Conditional Formats are volatile I would not recommend applying them to an entire column as every time Excel recalculates for any reason the CF will have to evaluate all 2 x 1,048,576 CF formulas to determine if the cell should be formatted a particular way.

  2. If you are going to apply the CF to cells below where the data is then a slight adjustment to the formula might be in order anyway.

So, in my dummy example below I have data in rows 2:12 but I want to allow for possible data down to row 20. I select from D2 (not D1) down to D20 and apply the shown CF formulas.
Is that something that you could adapt to suit your circumstances?

23 01 26.xlsm
D
1Number
2a5
3a5
4a5
5b3
6b3
77
87
97
10d6
11e45
12e45
13
14
15
16
17
18
19
20
Bands (2)
Cells with Conditional Formatting
CellConditionCell FormatStop If True
D2:D20Expression=AND(ISEVEN(ROWS(UNIQUE(D$2:D2))),D2<>"")textNO
D2:D20Expression=AND(ISODD(ROWS(UNIQUE(D$2:D2))),D2<>"")textNO
 
Upvote 0
Solution

Forum statistics

Threads
1,223,885
Messages
6,175,187
Members
452,616
Latest member
intern444

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