Find Groups and Highlight them

motilulla

Well-known Member
Joined
Feb 13, 2008
Messages
2,422
Office Version
  1. 2010
Using Excel 2010

I have some set of 6 numbers in the range A2:F24 I want to find groups of consecutive numbers (2, 3, 4, 5 and 6) in the range H2:L2 and then highlight groups in different colours in the range A2:F24. Need a VBA solution.

n1n2n3n4n5n6Count Consequtives Group of 2 NumbersCount Consequtives Group of 3 NumbersCount Consequtives Group of 4 NumbersCount Consequtives Group of 5 NumbersCount Consequtives Group of 6 Numbers
1​
2​
3​
4​
5​
6​
1
1​
2​
10​
12​
22​
35​
1
1​
2​
3​
34​
36​
38​
1
1​
8​
20​
24​
33​
35​
1​
10​
14​
24​
28​
35​
1​
11​
16​
17​
29​
33​
1
2​
12​
33​
34​
35​
36​
1
14​
15​
18​
19​
34​
35​
3
16​
17​
18​
22​
23​
24​
2
2​
19​
20​
21​
22​
36​
1
4​
27​
29​
31​
36​
38​
5​
7​
10​
17​
21​
32​
5​
7​
15​
16​
17​
39​
1
6​
8​
12​
18​
20​
27​
8​
9​
10​
22​
24​
36​
1
8​
10​
16​
25​
26​
39​
1
8​
11​
17​
20​
22​
34​
8​
9​
10​
11​
12​
37​
1
12​
19​
23​
28​
36​
39​
13​
16​
25​
28​
36​
38​
16​
19​
28​
30​
31​
38​
1
17​
21​
26​
27​
34​
36​
1
18​
34​
35​
36​
37​
39​
1

Regards,
Moti
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Hello, good morning here is an image attached for more in detailed in column A to F in each row there is set of 6 numbers. I want to get count of consecutive occurrences of numbers in the column H to L

And also VBA highlight the consecutive groups (2, 3, 4, 5 and 6) in the column A:F as shown in the image.

Regards,
Moti
 

Attachments

  • Groups.png
    Groups.png
    31.6 KB · Views: 12
Upvote 0
Hello, I have some set of 6 numbers in the range A2:F24 I want to find groups of consecutive numbers (2, 3, 4, 5 and 6) in the range H2:L2 and then highlight groups in different colors in the range A2:F24. Need a VBA solution.

If the highlight groups is not possible with VBA please help to count only consecutive groups in the range H2:L2.

Thank you all in advance

Regards,
Moti
 
Upvote 0
Give this a try.

VBA Code:
Sub Count_Groups()
  Dim a As Variant, b As Variant
  Dim r As Long, c As Long, i As Long, j As Long, k As Long, Clr As Long
  
  Clr = 5
  With Range("A2", Range("F" & Rows.Count).End(xlUp))
    .Font.ColorIndex = 0
    a = .Value
    ReDim b(1 To UBound(a), 2 To 6)
    For r = 1 To UBound(a)
      k = 1
      For c = 2 To 6
        If a(r, c) = a(r, c - 1) + 1 Then
          k = k + 1
        ElseIf k > 1 Then
          b(r, k) = b(r, k) + 1
          Clr = 8 - Clr
          For j = 1 To k
            .Cells(r, c - j).Font.ColorIndex = Clr
          Next j
          k = 1
        End If
      Next c
      If k > 1 Then
        b(r, k) = b(r, k) + 1
        Clr = 8 - Clr
        For j = 1 To k
          .Cells(r, c - j).Font.ColorIndex = Clr
        Next j
      End If
    Next r
  End With
  Range("H2:L2").Resize(UBound(b)).Value = b
End Sub

My worksheet before the code:

motilulla.xlsm
ABCDEFGHIJKL
1n1n2n3n4n5n6Groups of 2Groups of 3Groups of 4Groups of 5Groups of 6
2123456
31210122235
4123343638
51820243335
611014242835
711116172933
821233343536
9141518193435
10161718222324
1121920212236
1242729313638
135710172132
145715161739
156812182027
168910222436
1781016252639
1881117202234
198910111237
20121923283639
21131625283638
22161928303138
23172126273436
24183435363739
Sheet1



After:

motilulla.xlsm
ABCDEFGHIJKL
1n1n2n3n4n5n6Groups of 2Groups of 3Groups of 4Groups of 5Groups of 6
21234561
312101222351
41233436381
51820243335
611014242835
7111161729331
8212333435361
91415181934353
101617182223242
11219202122361
1242729313638
135710172132
1457151617391
156812182027
1689102224361
17810162526391
1881117202234
1989101112371
20121923283639
21131625283638
221619283031381
231721262734361
241834353637391
Sheet1
 
Upvote 1
Cheers. Glad it helped. :)

It will not do any harm but after testing the code I did not need the variable "i" so should have removed that declaration from the code but forgot. You can remove it if you want.
Rich (BB code):
Dim r As Long, c As Long, i As Long, j As Long, k As Long, Clr As Long
 
Upvote 1
Happened to be looking back over this code and noticed some other simplifications/shortening that could be applied if you want to try this version.

VBA Code:
Sub Count_Groups_v2()
  Dim a As Variant, b As Variant
  Dim r As Long, c As Long, k As Long, Clr As Long
  
  Clr = 5
  With Range("A2", Range("F" & Rows.Count).End(xlUp))
    .Font.ColorIndex = 0
    a = .Value
    ReDim b(1 To UBound(a), 2 To UBound(a, 2))
    ReDim Preserve a(1 To UBound(a), 1 To UBound(a, 2) + 1)
    For r = 1 To UBound(a)
      k = 1
      For c = 2 To UBound(a, 2)
        If a(r, c) = a(r, c - 1) + 1 Then
          k = k + 1
        ElseIf k > 1 Then
          b(r, k) = b(r, k) + 1
          Clr = 8 - Clr
          .Cells(r, c - k).Resize(, k).Font.ColorIndex = Clr
          k = 1
        End If
      Next c
    Next r
  End With
  Range("H2").Resize(UBound(b), UBound(b, 2) - 1).Value = b
End Sub
 
Upvote 1
Solution
Happened to be looking back over this code and noticed some other simplifications/shortening that could be applied if you want to try this version.
Peter, I appreciate your help, great of you for making shooter and better version. defiantly I am using this one. (y)

Good Luck forever!

My Best Regards,
Moti :)
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,197
Members
453,021
Latest member
pingpong7117

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