Conditional Formatting - How to Highlight cells in a row (Up to 5) if they have consecutive "ascending" numbers with different colors.

rennesan

New Member
Joined
Feb 19, 2021
Messages
3
Office Version
  1. 2016
Platform
  1. Windows
Conditional Formatting - How to Highlight cells in a row (Up to 5) if they have consecutive "ascending" numbers with different colors based on the matching criteria.

For example:

Sequence Numbers (50,57,58,60,70), (101,102,103,200,205), (300, 402,403,404,405) and (7,8,9,10,11)

If 2 consecutive numbers are found, consecutive numbers are highlighted GREEN (50,57,58,60,70)
If 3 consecutive numbers are found, consecutive numbers are highlighted YELLOW (101,102,103,200,205)
If 4 consecutive numbers are found, consecutive numbers are highlighted ORANGE (300, 402,403,404,405)
If 5 consecutive numbers are found, consecutive numbers are highlighted RED (7,8,9,10,11)

1613779170376.png


If color coding to this extent is not a possibility, at least highlight the consecutive numbers with one color.

Thank you in Advance,

Renne
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Welcome to the MrExcel board!

at least highlight the consecutive numbers with one color.
That is not so bad. Assuming we can have an empty column on each side of the data, try this.

21 02 20.xlsm
ABCDEFG
15057586070
2101102103200205
3300402403404405
47891011
CF Seq
Cells with Conditional Formatting
CellConditionCell FormatStop If True
B1:F4Expression=OR(AND(B1=A1+1,A1<>""),AND(B1=C1-1,C1<>""))textNO
 
Upvote 0
You could also try this vba code. It could be made more dynamic like CF if required if we knew just where the data is, if it is always 5 columns, if it is manually entered or evaluated by formula etc.
If you want to test this, make a copy of your data and remove any existing Conditional Formatting.

VBA Code:
Sub Recolour()
  Dim a As Variant, Clrs As Variant
  Dim i As Long, j As Long, fc As Long, k As Long, uba2 As Long
 
  Clrs = Split("0 0 10 6 45 3")
  Application.ScreenUpdating = False
  With Range("A1", Range("E" & Rows.Count).End(xlUp))
    .Interior.Color = xlNone
    a = .Value
    uba2 = UBound(a, 2)
    For i = 1 To UBound(a)
      k = 1
      fc = 1
      For j = 2 To uba2
        If a(i, j) = a(i, j - 1) + 1 Then
          k = k + 1
          If k > 1 Then .Cells(i, fc).Resize(, k).Interior.ColorIndex = Clrs(k)
        Else
          fc = j
          k = 1
        End If
      Next j
    Next i
  End With
  Application.ScreenUpdating = True
End Sub

rennesan.xlsm
ABCDE
15057586070
2101102103200205
3300402403404405
47891011
589122021
CF Seq (2)
 
Upvote 0
Solution
Welcome to the MrExcel board!


That is not so bad. Assuming we can have an empty column on each side of the data, try this.

21 02 20.xlsm
ABCDEFG
15057586070
2101102103200205
3300402403404405
47891011
CF Seq
Cells with Conditional Formatting
CellConditionCell FormatStop If True
B1:F4Expression=OR(AND(B1=A1+1,A1<>""),AND(B1=C1-1,C1<>""))textNO
WOW!!! That was a quick response!!! I tested out this and worked great!

Thank you very much!

Renné

PS - I will test out the VB Script tomorrow. Thank you again!
 
Upvote 0
You could also try this vba code. It could be made more dynamic like CF if required if we knew just where the data is, if it is always 5 columns, if it is manually entered or evaluated by formula etc.
If you want to test this, make a copy of your data and remove any existing Conditional Formatting.

VBA Code:
Sub Recolour()
  Dim a As Variant, Clrs As Variant
  Dim i As Long, j As Long, fc As Long, k As Long, uba2 As Long

  Clrs = Split("0 0 10 6 45 3")
  Application.ScreenUpdating = False
  With Range("A1", Range("E" & Rows.Count).End(xlUp))
    .Interior.Color = xlNone
    a = .Value
    uba2 = UBound(a, 2)
    For i = 1 To UBound(a)
      k = 1
      fc = 1
      For j = 2 To uba2
        If a(i, j) = a(i, j - 1) + 1 Then
          k = k + 1
          If k > 1 Then .Cells(i, fc).Resize(, k).Interior.ColorIndex = Clrs(k)
        Else
          fc = j
          k = 1
        End If
      Next j
    Next i
  End With
  Application.ScreenUpdating = True
End Sub

rennesan.xlsm
ABCDE
15057586070
2101102103200205
3300402403404405
47891011
589122021
CF Seq (2)
This VB Script also worked really well!!!
 
Upvote 0

Forum statistics

Threads
1,214,375
Messages
6,119,168
Members
448,870
Latest member
max_pedreira

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