Macro To Colour In Rows

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,786
Office Version
  1. 365
Platform
  1. Windows
I have upto 10000 rows with data in. I need the macro to look at the data in column 'C' and whenever the data changes I need it to change colour and colour in the whole row.

i.e C1:C10 has the same data and the macro colours in the rows red, C11:C20 the data is different so the rows get coloured yellow and so on. I know if there are 10000 rows it would be a lot of colours, but after say using 10 colours in can start again.

Thanks.
 

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".
No some may be 4, some maybe 15! They are all random.
 
Upvote 0
You'll have to choose more colors, but I think this'll work. If you can't

Code:
Sub color_the_rows()
    Dim Colors As Variant, SameData As String, i As Long, j As Integer
    Set Colors = Array(vbRed, vbGreen, vbYellow, vbBlue, vbWhite,vbMagenta,vbCyan)
    SameData = Cells(1, 3)
    For i = 1 To Cells(Rows.Count, 3).End(xlUp).Row
        If Cells(i, 3) <> SameData Then
            j = j + 1
        End If
        If j = 7 Then
            j = 0
            SameData = cells(i,3)
        End If
        Rows(i).Select
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = Colors(j)
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
    Next i
End Sub
 
Last edited:
Upvote 0
An error has come up, pointing at the colours part.
 
Upvote 0
USE THIS ONE!!! I'm kinda tweaking on caffeine right now
Code:
Sub color_the_rows()
    Dim Colors As Variant, SameData As String, i As Long, j As Integer
    Colors = Array(vbRed, vbGreen, vbYellow, vbBlue, vbWhite, vbMagenta, vbCyan)
    SameData = Cells(1, 3)
    For i = 1 To Cells(Rows.Count, 3).End(xlUp).Row
        If Cells(i, 3) <> SameData Then
            j = j + 1
            SameData = Cells(i, 3)
        End If
        If j = 7 Then
            j = 0
        End If
        Rows(i).Select
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = Colors(j)
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
    Next i
End Sub
 
Upvote 0
Thats it thanks, I was just writing that it was only colouring every other row when you sent me another. Thanks for your help.
 
Upvote 0
one more thing... if you ever decide to add in extra colors, or lessen them.

change this line
Code:
If j = 7 Then

to this
Code:
if j > ubound(colors) then
 
Upvote 0
What about if I want to change the column? Is that wherever there is a 3 in the code? So column D I would change to a 4 and so on?
 
Upvote 0

Forum statistics

Threads
1,224,534
Messages
6,179,390
Members
452,909
Latest member
VickiS

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