Coloring column cells based on values

Deepk

Board Regular
Joined
Mar 21, 2018
Messages
105
Office Version
  1. 2016
Platform
  1. Windows
Hi team,

I need a macro that fill or not fill color (grey) to the cells of two adjacent columns based on change in values of the second column. Please see the example below. Here both cells in a row are either filled or not filled(including the numbered one).

grey color1
grey color1
no color2
no color2
no color2
grey color3
grey color3
no color4

<tbody>
</tbody>

Also, I need second macro that work opposite to the above macro. Means, based on the color filled or not filled it assigns number in the previous column. see example below.

1grey filled
2no fill
2no fill
3grey filled
3grey filled
3grey filled
4no fill
5grey filled

<tbody>
</tbody>
Help appreciated!

Thanks
Deepk
 
Last edited:
Try this one for the second inquiry :

Code:
Sub FillNumbers()
    firstVal = 0
    cellColor = 0
    
    For Each c In Range("B2:B" & Range("B" & Rows.Count).End(xlUp).Row)
        
        If c.Interior.Color <> cellColor Then
            firstVal = firstVal + 1
            cellColor = c.Interior.Color
        End If
        Cells(c.Row, 1).Value = firstVal
        
    Next c
End Sub

A heartiest thank you Peter. Both codes are running fine. :)
 
Upvote 0

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Code:
Sub FillGreyColor()
    
    firstVal = 0
    modulo = 0
    
    Range("A:A").Interior.Color = xlNone
    
    For Each c In Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
        
        If c.Value <> firstVal Then
            modulo = modulo + 1
            firstVal = c.Value
        End If
        
        If modulo Mod 2 = 1 Then
            c.Interior.Color = 12566463
        End If
        
    Next c
End Sub

Hi louisH,

will it be possible to modify this code so that it can work for

work for selected range of cells in a column
color cells including the corresponding cells of next column
remove existing color if any in both columns before color them

Thank you.
 
Upvote 0
Hi louisH,

will it be possible to modify this code so that it can work for

work for selected range of cells in a column
color cells including the corresponding cells of next column
remove existing color if any in both columns before color them

Thank you.

Yes I can tinker Something like that :
Changes made :

-It will only work for the selected range of cells
-It will remove color whithin selection
-It will color selection + column to the right of selection

Code:
Sub FillGreyColor()
    
    firstVal = 0
    modulo = 0
    
    Dim col As Double
    col = Selection.Column
            
    If Selection.Columns.Count > 1 Then
        MsgBox "Select only 1 column"
        Exit Sub
    End If
    
    Selection.Interior.Color = xlNone
    Selection.Offset(0, 1).Interior.Color = xlNone
    
    For Each c In Selection
        
        If c.Value <> firstVal Then
            modulo = modulo + 1
            firstVal = c.Value
        End If
        
        If modulo Mod 2 = 1 Then
            Range(Cells(c.Row, col), Cells(c.Row, col + 1)).Interior.Color = 12566463
        End If
        
    Next c
    
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,515
Messages
6,119,974
Members
448,934
Latest member
audette89

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