VBA Change Font Color Based on Value

unknownymous

Board Regular
Joined
Sep 19, 2017
Messages
249
Office Version
  1. 2016
Platform
  1. Windows
Hello All,

I'm trying to write code that allows me to make negative numbers purple, positive numbers as orange and unchange as black. Basically, I need to reflect this for all sheets particularly Column L:N except for the tab sheetscalled "Results" and "Others".

Colors for Change:
Positive - RGB 102,0,102 | Number Format = 1,234
Negative - RGB 151,71,6 | Number Format = -1,234
Unchange - RGB 35,35,35 | Number Format = 0

Thanks!
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
First, I have to say I love your moniker! 🤘

And please give this macro a try. I wasn't sure how your criteria is accounted for for the "Unchanged" category, so I'm assuming that's synonymous with a 0 value.

(Also, since the colors were all rather dark, I added a line to make the font color white)

VBA Code:
Sub Unknownymous()
'Crafted by Wookiee at MrExcel.com


Dim rngCell As Range

For Each rngCell In Selection

  rngCell.NumberFormat = "#,##0;-#,##0"
  rngCell.Font.Color = vbWhite  'for better legibility!

  Select Case Sgn(rngCell.Value)

    Case Is = -1
      rngCell.Interior.Color = RGB(151, 71, 6)

    Case Is = 0
      rngCell.Interior.Color = RGB(35, 35, 35)

    Case Is = 1
      rngCell.Interior.Color = RGB(102, 0, 102)

  End Select

Next rngCell

End Sub[ATTACH type="full"]93175[/ATTACH]
 

Attachments

  • 1686234832753.png
    1686234832753.png
    2.9 KB · Views: 6
Upvote 0
Hello! Try this:
VBA Code:
Sub uknnms()
Dim sh As Worksheet, c As Range
Application.ScreenUpdating = 0
    For Each sh In ActiveWorkbook.Worksheets
        If sh.Name <> "Results" And sh.Name <> "Others" Then
            For Each c In sh.Range("L1:N" & sh.Rows.Count)
                If Not IsEmpty(c) And IsNumeric(c.Value) Then
                    Select Case c.Value
                        Case Is > 0:
                            c.Font.Color = RGB(102, 0, 102)
                        Case Is < 0:
                            c.Font.Color = RGB(151, 71, 6)
                        Case Else:
                            c.Font.Color = RGB(35, 35, 35)
                    End Select
                End If
            Next c
        End If
    Next sh
Application.ScreenUpdating = 1
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,151
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top