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

Posted on 2016-07-25
Last Modified: 2016-07-29
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
Question by:majervis
1 Comment
LVL 68

Accepted Solution

Qlemo earned 500 total points
ID: 41728534
You should be able to set .Interior.Color to a RGB value like you do with the font.

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This is an Add-On procedure to be used in conjunction with the code provided in Reducing EE Email Clutter using Outlook (…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now