Find header group in the whole column data, and fill colours

Kishan

Well-known Member
Joined
Mar 15, 2011
Messages
1,648
Office Version
  1. 2010
Platform
  1. Windows
Using Excel 2000

Hi,

I want VBA, which look in to whole Column (C:P) data; header group is in the row 2, 3, and 4 through (C:P) and fill colour if matches finds

For example...
Column C header C1:C3 = ABC look in to whole column C, ABC, and fill colour
Column D header D1:D3 = ABD look in to whole column D, ABD, and fill colour
Same procedure for the next columns up to column P

Sheet example...


Book1
ABCDEFGHIJKLMNOPQ
1
2AACABDBAAAACBA
3BBAACCCBAAAADA
4CDABBABCAAAACA
51234567891011121314
6
7DBBADBCADCBBCA
8DCCADCBADBCCBA
9DAABDADACDBACA
10CBADCADAADDBAA
11BDBDBBCABCCCAB
12DCCDCCABCABABC
13CBBDABACABCADA
14ACCDBDBABDABDB
15ABBCCCCADCADDC
16BCDBABBBDAACDB
17CADCBCDCCABADC
18AACACBCAABDBCA
19BAABBCAAADCCBB
20CAACDABBADBADD
21AAABDACDADCADC
22BABCCBACADAACB
23CACAADAABCBBBC
24BAABACBACBCCCB
25CAACBACAACAAAC
26BAAACBAABBAABA
27CBBBBDABCCABCA
28ACCDCDBDBBADAB
29AAADBCDCDCBCBD
30AAADCACBCADACD
31AAADAAACAACAAC
32ABACBAABBBABBA
33BDAADABDCDBCDA
34CDABCACCACDADA
35BCACBBABBACBDA
36CBBBCCBDCABCDB
37BCCCAACCABDBDD
38DBAABBAAADDCCC
39DCBADCBBBDDAAB
40CACBDADDCDCBBC
41BBBCCBCCACBDCA
42DDDABCBAABCDBA
43DDCBDADAACACDA
44DDADCACABABACB
45CCBCABAACACBAC
46AACABDBAAAACBA
47BBAACCCBAAAADA
48CDABBABCAAAACA
49
Sheet1


Thank you in advance

Regards,
Kishan
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Like this?

Code:
Sub colorcells()

Dim rownum As Long
Dim rownum2 As Long
Dim colnum As Long


colnum = 3
Do Until colnum = 17


rownum = 1
rownum2 = 6
Do Until rownum2 = 49


color1 = Cells(rownum, colnum).Interior.Color
If Cells(rownum2, colnum).Value = Cells(rownum, colnum) And Cells(rownum2 + 1, colnum) = Cells(rownum + 1, colnum) And Cells(rownum2 + 2, colnum) = Cells(rownum + 2, colnum) Then
Cells(rownum2, colnum).Interior.Color = color1
Cells(rownum2 + 1, colnum).Interior.Color = color1
Cells(rownum2 + 2, colnum).Interior.Color = color1
End If


rownum2 = rownum2 + 1
Loop


colnum = colnum + 1
Loop


End Sub

*just realised this won't deal with the likes of AAA in a AAAA scenario... Looking again
 
Last edited:
Upvote 0
This seems to be working:

Code:
Sub colorcells()

Dim rownum As Long
Dim rownum2 As Long
Dim colnum As Long


colnum = 3
Do Until colnum = 17


rownum = 1
rownum2 = 6
Do Until rownum2 = 49


color1 = Cells(rownum, colnum).Interior.Color
If Cells(rownum2, colnum).Value = Cells(rownum, colnum) And Cells(rownum2 + 1, colnum) = Cells(rownum + 1, colnum) And Cells(rownum2 + 2, colnum) = Cells(rownum + 2, colnum) Then
Cells(rownum2, colnum).Interior.Color = color1
Cells(rownum2 + 1, colnum).Interior.Color = color1
Cells(rownum2 + 2, colnum).Interior.Color = color1
rownum2 = rownum2 + 2
End If


rownum2 = rownum2 + 1
Loop


colnum = colnum + 1
Loop


End Sub
 
Upvote 0
Maybe...

Assumes that cells in the range C2:P4 are already colored

Code:
Sub aTest()
    Dim rCrit As Range, rData As Range
    Dim i As Long, j As Long
    
    Set rCrit = Range("C2:P4")
    Set rData = Range("C7:P" & Cells(Rows.Count, "C").End(xlUp).Row)
    
    For i = 1 To rData.Columns.Count
        With rData.Columns(i)
            j = 1
            Do
                If .Cells(j) = rCrit.Columns(i).Cells(1) _
                        And .Cells(j + 1) = rCrit.Columns(i).Cells(2) _
                        And .Cells(j + 2) = rCrit.Columns(i).Cells(3) Then
                    .Cells(j).Resize(3).Interior.Color = rCrit.Columns(i).Cells(1).Interior.Color
                    j = j + 3
                Else
                    j = j + 1
                End If
            Loop Until j > rData.Rows.Count - 2
        End With
    Next i
End Sub

M.
 
Upvote 0
Maybe...

Assumes that cells in the range C2:P4 are already colored

Code:
Sub aTest()
    Dim rCrit As Range, rData As Range
    Dim i As Long, j As Long
    
    Set rCrit = Range("C2:P4")
    Set rData = Range("C7:P" & Cells(Rows.Count, "C").End(xlUp).Row)
    
    For i = 1 To rData.Columns.Count
        With rData.Columns(i)
            j = 1
            Do
                If .Cells(j) = rCrit.Columns(i).Cells(1) _
                        And .Cells(j + 1) = rCrit.Columns(i).Cells(2) _
                        And .Cells(j + 2) = rCrit.Columns(i).Cells(3) Then
                    .Cells(j).Resize(3).Interior.Color = rCrit.Columns(i).Cells(1).Interior.Color
                    j = j + 3
                Else
                    j = j + 1
                End If
            Loop Until j > rData.Rows.Count - 2
        End With
    Next i
End Sub

M.
Hi Marcelo Branco, I like your way of colouring, VBA-Solution!! It is very practical, excellent idea!!

I tried by changing the range C2:P4, with different colours and it is resulting spot on!!

Thank you very much for your time and help

Regards,
Kishan
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,830
Messages
6,181,227
Members
453,025
Latest member
Hannah_Pham93

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