• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 64
  • Last Modified:

Would like to assign RGB colors for fill, text and border based on text values for a range of values using VBA

I was given code a few years back to conditionally color a range of values based on text (e.g. book to price), but I need to expand beyond the icolor 57 limit limitation.  Any insights would be greatly appreciated.  Attached is the code that works using icolor.  Thank you.

' Revised worksheet code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("c241:k277")) Is Nothing Then
        ColourCells Intersect(Target, Range("c241:k1277"))
    End If
End Sub
Sub ColourThemAll3()
    ColourCells ActiveSheet.Range("c134:k152")
End Sub
Sub ColourCells(rngIn As Range)
    Dim rngcell As Range
    Dim iColor As Long
    For Each rngcell In rngIn
        With rngcell
            Select Case .Value
                Case "Book Value per Share to Price", "Cashflow per Share to Price", "Dividend Yield", "Earnings per Share to Price", "EBITDA to EV", "EBITDA to Price", "IBES Dividend Yield", "IBES Earnings Yield", "IBES Sales Yield", "Sales per Share to Price", "Sales to EV", "Free Cashflow Yield"
                    iColor = 5
                    .Font.Color = RGB(255, 255, 255)
                Case "Growth in Earnings per Share", "IBES 12 Month Forward  Growth in Earnings per Share", "IBES Earnings Long Term Growth", "IBES FY1  Earnings Revisions 1M Sample", "IBES FY1  Earnings Revisions 3M Sample", "IBES FY2  Earnings Revisions 1M Sample", "IBES FY2  Earnings Revisions 3M Sample", "IBES Sales 12 Mth Growth", "IBES Sales Long Term Growth", "Income to Sales", "Return on Equity", "Sales Growth", "Sustainable Growth Rate"
                    iColor = 10
                    .Font.Color = RGB(255, 255, 255)
                Case "Beta", "Market Cap (Large Cap)*"
                    iColor = 3
                    .Font.Color = RGB(255, 255, 255)
                Case "Momentum 12 Mth", "Momentum 6 Mth", "Momentum Short Term (6 Month Exp Wtd)"
                    iColor = 1
                    .Font.Color = RGB(255, 255, 255)
                Case "Debt to Equity", "Foreign Sales as a % Total Sales"
                    iColor = 36
                    .Font.Color = RGB(0, 0, 0)
                Case "Low Gearing", "Stability Of Earnings Growth", "Stability Of FY1 Revisions", "Stability Of IBES 12 Mth Growth Forecast", "Stability of Returns", "Stability Of Sales Growth"
                    iColor = 18
                    .Font.Color = RGB(255, 255, 255)
                Case Else
            End Select
            .Interior.ColorIndex = iColor
        End With
    Next rngcell
End Sub
1 Solution
QlemoC++ DeveloperCommented:
You should be able to set .Interior.Color to a RGB value like you do with the font.

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now