Forum Discussion

sirtajsingh's avatar
sirtajsingh
Copper Contributor
Feb 03, 2020
Solved

Excel Macros If Function Based on Cell Color

I don't know how to code in VBA but am trying to automate an if/then calculation based on cell color.   So I have many cells that are filled in with the RGB color code RGB (255, 235, 156). What I w...
  • Riny_van_Eekelen's avatar
    Feb 03, 2020

    sirtajsingh 

    Try this one:

     

    Sub ColorMacro()
    
    Dim myCell As Variant
    
    For Each myCell In Range("A1:G10000")
    
        If myCell.Interior.Color = 10284031 Then
        
            myCell.Font.Bold = True
            myCell.Font.Italic = True
            With myCell.Interior
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .Color = 16777215
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With
            
            With myCell.Font
                .Color = -16776961
                .TintAndShade = 0
            End With
            
        End If
        
    Next myCell
    
    End Sub

     

     The color value of your RGB values = 10284031 (i.e. the result of R+G*256+B*256*256). You need this to be able to read out color value in each of your cells. 

Resources