+ Reply to Thread
Results 1 to 7 of 7

Changing the font color of a cell based on value in another cell

Hybrid View

  1. #1
    Registered User
    Join Date
    07-26-2010
    Location
    Hawaii
    MS-Off Ver
    Excel 2010
    Posts
    25

    Changing the font color of a cell based on value in another cell

    I can get this to work on a single range of cells but I can’t get it to work on a second or third range of cells. My goal is to be able to change the “Goal” value in a primary cell and have the range of cells associated with that goal change colors from red, green, or black if they meet, exceed, or are below the goal.

    Goal 95% then any cell that is less than 95% the font color will be red.
    Goal 95% then any cell that is Greater than 95% the font color will be red.

    and so on...

    Here's the code I have at the moment and am currently getting a compile error of Next without For and I'm pretty much stuck at this point.

    Private Sub Worksheet_Change(ByVal Target As Range)
    
        Dim rng As Range
        Dim cell As Range
        Dim Stroke As Range
        Dim IMM As Range
        Dim Measure As String
        
        Set rng = Range("F4:K8")
        Set Stroke = Range("F4:K4")
        Set IMM = Range("F5:K5")
        'Measure = Range("D4")
        
        For Each cell In rng
        'Stroke
            If Stroke Then
                Measure = Range("D4")
            If cell = Measure Then
                cell.Font.Color = 0     'Black
            ElseIf cell > Measure Then
                cell.Font.Color = 32768 'Green
            ElseIf cell < Measure Then
                cell.Font.Color = 128   'Red
            End If
        'Immunization
            If IMM Then
                Measure = Range("D5")
            If cell = Measure Then
                cell.Font.Color = 0     'Black
            ElseIf cell > Measure Then
                cell.Font.Color = 32768 'Green
            ElseIf cell < Measure Then
                cell.Font.Color = 128   'Red
            End If
        Next cell
        
    End Sub
    Any help is appreciated, thanks.

  2. #2
    Registered User
    Join Date
    07-26-2010
    Location
    Hawaii
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Changing the font color of a cell based on value in another cell

    I fixed the Loop error but it's still not working as intended.

  3. #3
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,943

    Re: Changing the font color of a cell based on value in another cell

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    
        Dim rng As Range
        Dim cell As Range
        Dim Stroke As Range
        Dim IMM As Range
        Dim Measure As String
        
        Set rng = Range("F4:K8")
        Set Stroke = Range("F4:K4")
        Set IMM = Range("F5:K5")
        'Measure = Range("D4")
        
        For Each cell In rng
        'Stroke
            If Stroke Then
                Measure = Range("D4")
                If cell = Measure Then
                    cell.Font.Color = 0     'Black
                ElseIf cell > Measure Then
                    cell.Font.Color = 32768 'Green
                ElseIf cell < Measure Then
                    cell.Font.Color = 128   'Red
                End If  'cell = measure
            End If 'missing stroke test
        'Immunization
            If IMM Then
                Measure = Range("D5")
                If cell = Measure Then
                    cell.Font.Color = 0     'Black
                ElseIf cell > Measure Then
                    cell.Font.Color = 32768 'Green
                ElseIf cell < Measure Then
                    cell.Font.Color = 128   'Red
                End If  ' end if cell = measure
            End If 'missing for IMM test
        Next cell
    End Sub
    Last edited by protonLeah; 01-26-2017 at 04:45 PM.
    Ben Van Johnson

  4. #4
    Registered User
    Join Date
    07-26-2010
    Location
    Hawaii
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Changing the font color of a cell based on value in another cell

    Thanks you that cleared up the error but it's still not changing the cell font colors.

  5. #5
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Changing the font color of a cell based on value in another cell

    I don't understand the following line:

    If Stroke Then
    Stroke is defined as a range of addresses, which does not have a value, I get a runtime error on that line. Elements of the range have values, but the range itself does not have a value.

    Please let us know what you are trying to do with that line.

    Lewis

  6. #6
    Registered User
    Join Date
    07-26-2010
    Location
    Hawaii
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Changing the font color of a cell based on value in another cell

    I thought that could be in issue.

    I dimmed Stroke as range then set the range
    Set Stroke = Range("F4:K4")

    I'm calling that stroke range to set the measure value for that range. I don’t actually know if that’s the proper/correct way to call it.

  7. #7
    Registered User
    Join Date
    07-26-2010
    Location
    Hawaii
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Changing the font color of a cell based on value in another cell

    SOLVED!

    Private Sub Worksheet_Change(ByVal Target As Range)
    
        Dim StrokeRange As Range
        Dim IMMRange As Range
        Dim cell As Range
        Dim Measure As String
        
        Set StrokeRange = Range("F4:K4")
        Set IMMRange = Range("F5:K5")
        'Measure = Range("D4")
        
        For Each cell In StrokeRange
          Measure = Range("D4")
            If cell = Measure Then
                cell.Font.Color = 0     'Black
            ElseIf cell > Measure Then
                cell.Font.Color = 32768 'Green
            ElseIf cell < Measure Then
                cell.Font.Color = 128   'Red
         End If
        Next cell
    
        For Each cell In IMMRange
          Measure = Range("D5")
            If cell = Measure Then
                cell.Font.Color = 0     'Black
            ElseIf cell > Measure Then
                cell.Font.Color = 32768 'Green
            ElseIf cell < Measure Then
                cell.Font.Color = 128   'Red
         End If
        Next cell
        
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Changing font color based on date value in another cell
    By winwall in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-07-2014, 10:28 AM
  2. Change font color based on font color of another cell
    By Ibyers in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2012, 09:36 AM
  3. Replies: 1
    Last Post: 06-28-2012, 09:56 AM
  4. Changing font color in sum cell
    By Sindee123 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 03-09-2012, 12:31 PM
  5. Replies: 7
    Last Post: 05-26-2011, 03:44 PM
  6. Changing font color of active cell
    By ridesisapis in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-11-2009, 08:10 PM
  7. [SOLVED] changing font color of cell content
    By Donald Stockton in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 03-09-2006, 01:55 PM
  8. [SOLVED] Changing Cell Font and Color Issue,
    By RitaG in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-31-2005, 08:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1