Forum Discussion
sirtajsingh
Feb 03, 2020Copper Contributor
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...
- Feb 03, 2020
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.
Riny_van_Eekelen
Feb 03, 2020Platinum Contributor
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.
sirtajsingh
Feb 03, 2020Copper Contributor
Riny_van_Eekelen Thank you so much, that worked perfectly 🙂