Look up in row constant occurrences

motilulla

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

I am looking VBA solution for specific task look up in row constant occurrences

Data got in cells C5:P22, "1" is filled with red colour and white fonts, "X" is filled with dark green colour and white fonts, "2" is filled with blue colour and white fonts,

Results shown in R6 AF22

Example for row 6...
C6 = X count constant occurrence =1, Result cell R6 =1 as it is "X" so fill dark green colour and white fonts

D6 = 1 count constant occurrence =1, Result cell S6 =1 as it is "1" so fill red colour and white fonts

E, F, G 6 = 2 count constant occurrence = 3, Result cell T6 =3 as it is "2" so fill blue colour and white fonts


Book1
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAG
1
2
3
4
5P1P2P3P4P5P6P7P8P9P10P11P12P13P14P1P2P3P4P5P6P7P8P9P10P11P12P13P14P15
6X12221111X11221134122
7221X1X2XX12X21211111211111
8XX1212X112XXX12111112131
92211X2X1X12XX222111111121
1012X1X1111X1112111114131
11122X121122111112111224
12XX12XX221111X221122411
1321121X1111121112111512
14122222121211111511114
152X222111X1X112113311121
16111211112X1X1131411112
172212X2X1X111X221111111311
18221X12211X11X12111221211
19X222221211112X1511411
20121121111XX2X1112142111
21XXXXXXXXXXXXX1131
22111111X1111111617
23
24
Sheet5


Resume: count constant occurrence and after counts, result colour cells as per filled in data range

Thank you all
Excel 2000
Regards,
Moti
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Try this:

Code:
Dim rngx As Range, rng1 As Range, rng2 As Range, c As Range
Dim lr As Long, a As Long, b As Long, i As Long, j As Long

'get last row of data
lr = Range("C" & Rows.Count).End(xlUp).Row
If lr < 6 Then Exit Sub

'clean up results section
With Range("R6:AF" & lr)
    .ClearContents
    .Interior.ColorIndex = xlNone
    .Font.Color = vbBlack
End With

'colour cells in data section
For Each c In Range("C6:P" & lr)
    c.Interior.ColorIndex = xlNone
    c.Font.Color = vbBlack
    Select Case c.Value
        Case "X"
            If Not rngx Is Nothing Then
                Set rngx = Union(rngx, c)
            Else
                Set rngx = c
            End If
        Case 1
            If Not rng1 Is Nothing Then
                Set rng1 = Union(rng1, c)
            Else
                Set rng1 = c
            End If
        Case 2
            If Not rng2 Is Nothing Then
                Set rng2 = Union(rng2, c)
            Else
                Set rng2 = c
            End If
    End Select
Next

If Not rngx Is Nothing Then
    With rngx.Cells
        .Font.Color = vbWhite
        .Interior.ColorIndex = 50
    End With
End If
If Not rng1 Is Nothing Then
    With rng1.Cells
        .Font.Color = vbWhite
        .Interior.Color = vbRed
    End With
End If
If Not rng2 Is Nothing Then
    With rng2.Cells
        .Font.Color = vbWhite
        .Interior.Color = vbBlue
    End With
End If

'loop through data section to colour results section
a = 1

For i = 6 To lr 'rows 6 to last row of data
    b = 1
    For j = 3 To 16 'columns C to P
        If Cells(i, j) = Cells(i, j + 1) Then
            a = a + 1
        Else
            With Cells(i, 17 + b)
                .Value = a
                .Font.Color = Cells(i, j).Font.Color
                .Interior.Color = Cells(i, j).Interior.Color
            End With
            a = 1
            b = b + 1
        End If
    Next
Next
 
Upvote 0
Another option:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG17Nov56
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] K [COLOR="Navy"]As[/COLOR] Variant, R [COLOR="Navy"]As[/COLOR] Range, ac [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Set[/COLOR] Rng = Range("C6", Range("C" & Rows.Count).End(xlUp))
[COLOR="Navy"]With[/COLOR] CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    [COLOR="Navy"]For[/COLOR] ac = 0 To 13
        [COLOR="Navy"]If[/COLOR] Not .Exists(Dn.Offset(, ac).Value) [COLOR="Navy"]Then[/COLOR]
            .Add Dn.Offset(, ac).Value, Dn.Offset(, ac)
        [COLOR="Navy"]Else[/COLOR]
            [COLOR="Navy"]Set[/COLOR] .Item(Dn.Offset(, ac).Value) = _
            Union(.Item(Dn.Offset(, ac).Value), Dn.Offset(, ac))
        [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] ac
ReDim Ray(1 To 14, 1 To 2)
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] K [COLOR="Navy"]In[/COLOR] .keys
    [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] R [COLOR="Navy"]In[/COLOR] .Item(K).Areas
       Ray(R(1).Column - 2, 1) = R.Count
       Ray(R(1).Column - 2, 2) = R.Interior.ColorIndex
    [COLOR="Navy"]Next[/COLOR] R
[COLOR="Navy"]Next[/COLOR] K
.RemoveAll
c = 0
[COLOR="Navy"]For[/COLOR] n = 1 To 14
    [COLOR="Navy"]If[/COLOR] Not IsEmpty(Ray(n, 1)) [COLOR="Navy"]Then[/COLOR]
        c = c + 1
        Dn.Offset(, 14 + c) = Ray(n, 1)
        Dn.Offset(, 14 + c).Interior.ColorIndex = Ray(n, 2)
        Dn.Offset(, 14 + c).Font.ColorIndex = 2
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] n
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Another option:-Regards Mick
Thanks MickG, I like your code, but I am sorry for my mistake not telling in the opening post about that data side column C:P colours were reflected by conditional format, so after running your code it gives perfect counts, as the fonts colour is white, which I can see highlighting the results area by mouse.

Please could you modify the code so it can fill the colours as described for 1 X & 2 in the result area, even if the data area is not filled with colours? I mean if it is simple with white background and black fonts.

Regards,
Moti
 
Upvote 0
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG17Nov19
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] K [COLOR="Navy"]As[/COLOR] Variant, R [COLOR="Navy"]As[/COLOR] Range, ac [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] col [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer[/COLOR]
[COLOR="Navy"]Set[/COLOR] Rng = Range("C6", Range("C" & Rows.Count).End(xlUp))
[COLOR="Navy"]With[/COLOR] Rng.Offset(, 15).Resize(, 14)
.Interior.ColorIndex = xlNone
.Font.ColorIndex = 2
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]With[/COLOR] CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    [COLOR="Navy"]For[/COLOR] ac = 0 To 13
        [COLOR="Navy"]If[/COLOR] Not .Exists(Dn.Offset(, ac).Value) [COLOR="Navy"]Then[/COLOR]
            .Add Dn.Offset(, ac).Value, Dn.Offset(, ac)
        [COLOR="Navy"]Else[/COLOR]
            [COLOR="Navy"]Set[/COLOR] .Item(Dn.Offset(, ac).Value) = _
            Union(.Item(Dn.Offset(, ac).Value), Dn.Offset(, ac))
        [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] ac
[COLOR="Navy"]Dim[/COLOR] t
ReDim Ray(1 To 14, 1 To 2)
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] K [COLOR="Navy"]In[/COLOR] .keys
    [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] R [COLOR="Navy"]In[/COLOR] .Item(K).Areas
       t = R.Address
       [COLOR="Navy"]Select[/COLOR] [COLOR="Navy"]Case[/COLOR] R(1).Value
        [COLOR="Navy"]Case[/COLOR] "X": col = 10
        [COLOR="Navy"]Case[/COLOR] 1: col = 3
        [COLOR="Navy"]Case[/COLOR] 2: col = 5
       [COLOR="Navy"]End[/COLOR] Select
       Ray(R(1).Column - 2, 1) = R.Count
       Ray(R(1).Column - 2, 2) = col
    [COLOR="Navy"]Next[/COLOR] R
[COLOR="Navy"]Next[/COLOR] K
.RemoveAll
c = 0
[COLOR="Navy"]For[/COLOR] n = 1 To 14
    [COLOR="Navy"]If[/COLOR] Not IsEmpty(Ray(n, 1)) [COLOR="Navy"]Then[/COLOR]
        c = c + 1
        Dn.Offset(, 14 + c) = Ray(n, 1)
        Dn.Offset(, 14 + c).Interior.ColorIndex = Ray(n, 2)
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] n
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Try this:-
Code:
[COLOR=navy]Sub[/COLOR] MG17Nov19
[COLOR=navy]Dim[/COLOR] Rng [COLOR=navy]As[/COLOR] Range, Dn [COLOR=navy]As[/COLOR] Range, n [COLOR=navy]As[/COLOR] [COLOR=navy]Long,[/COLOR] K [COLOR=navy]As[/COLOR] Variant, R [COLOR=navy]As[/COLOR] Range, ac [COLOR=navy]As[/COLOR] [COLOR=navy]Long,[/COLOR] c [COLOR=navy]As[/COLOR] [COLOR=navy]Long[/COLOR]
[COLOR=navy]Dim[/COLOR] col [COLOR=navy]As[/COLOR] [COLOR=navy]Integer[/COLOR]
[COLOR=navy]Set[/COLOR] Rng = Range("C6", Range("C" & Rows.Count).End(xlUp))
[COLOR=navy]With[/COLOR] Rng.Offset(, 15).Resize(, 14)
.Interior.ColorIndex = xlNone
.Font.ColorIndex = 2
[COLOR=navy]End[/COLOR] With
[COLOR=navy]With[/COLOR] CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
[COLOR=navy]For[/COLOR] [COLOR=navy]Each[/COLOR] Dn [COLOR=navy]In[/COLOR] Rng
    [COLOR=navy]For[/COLOR] ac = 0 To 13
        [COLOR=navy]If[/COLOR] Not .Exists(Dn.Offset(, ac).Value) [COLOR=navy]Then[/COLOR]
            .Add Dn.Offset(, ac).Value, Dn.Offset(, ac)
        [COLOR=navy]Else[/COLOR]
            [COLOR=navy]Set[/COLOR] .Item(Dn.Offset(, ac).Value) = _
            Union(.Item(Dn.Offset(, ac).Value), Dn.Offset(, ac))
        [COLOR=navy]End[/COLOR] If
[COLOR=navy]Next[/COLOR] ac
[COLOR=navy]Dim[/COLOR] t
ReDim Ray(1 To 14, 1 To 2)
[COLOR=navy]For[/COLOR] [COLOR=navy]Each[/COLOR] K [COLOR=navy]In[/COLOR] .keys
    [COLOR=navy]For[/COLOR] [COLOR=navy]Each[/COLOR] R [COLOR=navy]In[/COLOR] .Item(K).Areas
       t = R.Address
       [COLOR=navy]Select[/COLOR] [COLOR=navy]Case[/COLOR] R(1).Value
        [COLOR=navy]Case[/COLOR] "X": col = 10
        [COLOR=navy]Case[/COLOR] 1: col = 3
        [COLOR=navy]Case[/COLOR] 2: col = 5
       [COLOR=navy]End[/COLOR] Select
       Ray(R(1).Column - 2, 1) = R.Count
       Ray(R(1).Column - 2, 2) = col
    [COLOR=navy]Next[/COLOR] R
[COLOR=navy]Next[/COLOR] K
.RemoveAll
c = 0
[COLOR=navy]For[/COLOR] n = 1 To 14
    [COLOR=navy]If[/COLOR] Not IsEmpty(Ray(n, 1)) [COLOR=navy]Then[/COLOR]
        c = c + 1
        Dn.Offset(, 14 + c) = Ray(n, 1)
        Dn.Offset(, 14 + c).Interior.ColorIndex = Ray(n, 2)
    [COLOR=navy]End[/COLOR] If
[COLOR=navy]Next[/COLOR] n
[COLOR=navy]Next[/COLOR] Dn
[COLOR=navy]End[/COLOR] With
[COLOR=navy]End[/COLOR] [COLOR=navy]Sub[/COLOR]
Regards Mick
MickG, this one is very fine! :)

Thank you very much for your time and for the quick modifications

Have a nice day

Regards,
Moti
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,252
Members
452,623
Latest member
Techenthusiast

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