Assigning numerical values based on font colors

gary2068

New Member
Joined
Jul 18, 2023
Messages
3
Office Version
  1. 2013
Platform
  1. Windows
In excel 2013, is it possible to automatically assign a numerical value in a given cell, let's say cell X10, based on the font color of a different cell, lets say V12? The font color can change in cell V12 up to 10 different colors based on data entries from a seperate location on the spreadsheet.

For example, if the font in V12 = blue then 1 in X10, if the font color in V12 = red then 2 in X10....and so on?

Also, the value in cell V12 is generated thru a macro (if that makes any difference)

Thanks in advance for your help.
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Welcome to the Board!

Excel formulas can only run off of values in cells, not on the formatting (like color) of cells.
So to do what you want would require some sort of VBA.

Are you open to a VBA solution?
We can either create a Macro (procedure), or a User Defined Function in VBA (our own custom function that you would use like other functions).
 
Upvote 0
Sure....VBA is fine. Pls keep in mind that the numerical value needs to be assigned to the font color and not the cell color. thanks
 
Upvote 0
OK, insert a new VBA module into your current workbook, and paste this code in it:
VBA Code:
Function ColorCheck(cell As Range) As Long
'   Check the color of a cell and return a value depending on what it is

    Dim colorCode As Long
    
'   See how many cells in range
    If cell.Count > 1 Then
'       If more than one cell, set value to 0
        ColorCheck = 0
        Exit Function
    Else
'       Otherwise, capture the code of the current cell
        colorCode = cell.Font.Color
    End If
    
'   Determine which values to bring back
    Select Case colorCode
        Case 12611584 'blue font color
            ColorCheck = 1
        Case 255 'red font color
            ColorCheck = 2
        Case 0 'black font color
            ColorCheck = 3
        Case Else 'any other color
            ColorCheck = 99
    End Select

End Function

Note this particular section here:
VBA Code:
    Select Case colorCode
        Case 12611584 'blue font color
            ColorCheck = 1
        Case 255 'red font color
            ColorCheck = 2
        Case 0 'black font color
            ColorCheck = 3
        Case Else 'any other color
            ColorCheck = 99
    End Select
This is where you have each color code, and the associated value you want to return with it (i.e. if color is red, which is code 255, return a value of 2).
You will need to update this section for all your values. Note that you can insert more cases, just do it before the "Case Else", which should always be your last "Case" statement (i.e. this is what to do if NONE of the conditions above are met).

If you are unsure what the color codes are, here is an easy way to get them.
Change the font of cell A1 to the color you want to get the code for, then run this VBA code (you can paste it in the same VBA module as your function).
VBA Code:
Sub GetColor()
    MsgBox Range("A1").Font.Color
End Sub

Once you have the function completely written, you would just use it like any other function in Excel.
So if you wanted cell X10 to return a code based on the font color in cell V12, then you would just enter this formula in cell X10:
Excel Formula:
=ColorCheck(V12)
 
Upvote 0
Solution
You are welcome.
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,215,072
Messages
6,122,966
Members
449,094
Latest member
Anshu121

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