Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

color by value

0

I would like for each value in a column to be a different color. Any ideas?

Answer
Discuss

Discussion

What about just applying conditional formatting?
don (rep: 1989) Sep 9, '20 at 9:54 am
because i have a zillion different values. i want each value, irregardless of how many they are to be a differnt color.
joshie Sep 10, '20 at 6:21 am
Hi Joshie,
Variatus has provided (below) a great solution if you want this to be limited to a specific range of cells. If you want the cell fill to happen regardless of a specific range you can insert the following code in the WorkSheet code module. This code does not rely on other code to run. It includes an check so that if the cell value is not a number then nothing happens.

Private Sub Worksheet_Change(ByVal Target As Range)
 
Dim ColVal As Long   ' use number (value) of cell to select colour
Dim CellToFill As String   ' address of cell
 
If IsNumeric(Target.Value) Then   ' proceed if cell value is a number
' get cell value to use as fill colour number
CellToFill = Target.Address   ' currently selected cell
ColVal = Range(CellToFill).Value   ' number (value) in target cell
 
Range(CellToFill).Select   ' select target cell and fill cell with ColVal
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = ColVal
    End With
End If
 
End Sub


Now you have two options to choose from.

If you want to limit which cells this cell fill code works on you can the eliments as posted by Variatus to do so. (see his "Sub Worksheet_Change" code in his post)

Cheers   ;-}
WillieD24 (rep: 547) Sep 13, '20 at 1:41 pm
Great!!
Can you help me modify it, so I can paste it into a "insert" module. And the activation will be that when the macro is run, then it will ask me on which column to run the macro???
joshie Sep 15, '20 at 12:50 pm
Are you intending to add cell fill colour to just one specific column? A few specific columns? Or a Random column? If to a random column, how will it be decided? Are you looking to add fill to specific rows of the column or anywhere in the column? Is there a known limit to what the min/max of the cell value will be?
WillieD24 (rep: 547) Sep 15, '20 at 8:14 pm
WillieD can you please put your big response with the code into its own answer here so that future readers can better find it in case it is what they need? I think it might be a little tough for them to go through the discussion to find it.
don (rep: 1989) Sep 16, '20 at 2:16 am
thank-you WillieDee24, 
I understand that there are many variables to my question. However, I want specifially to choose ONE column, and have it colored per different value. So the macro will ask me "Which Column?" And run on that.
joshie Sep 16, '20 at 7:29 am
This is the code which is a combination of Variatus' post and my own. It gets put into a Worksheet code window and works with that sheet only.
Private Sub Worksheet_Change(ByVal Target As Range)
' original macro written by Willied24, Sept. 2020
' enhanced with some code posted by Variatus on www.teachexcel.com
Dim ColVal As Long   ' use number (value) of cell to select colour
Dim CellToFill As String   ' address of cell
Const TriggerRange  As String = "A2:A25"   ' limit the range of cells where fill will be applied
 
' skip if more than 1 cell was changed (like paste)
    With Target
        If .CountLarge > 1 Then Exit Sub
    End With
    
If IsNumeric(Target.Value) Then   ' proceed if cell value is a number
' get cell value to use as fill colour number
    If Not Application.Intersect(Target, Range(TriggerRange)) Is Nothing Then
        CellToFill = Target.Address   ' currently selected cell
        ColVal = Range(CellToFill).Value   ' number (value) in target cell
 
        Range(CellToFill).Select   ' select target cell and fill cell with ColVal
            With Selection.Interior
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .Color = ColVal   ' use number in cell to select colour
            End With
    End If
End If
 
End Sub

WillieD24 (rep: 547) Sep 16, '20 at 9:08 pm
Joshie,
You say you want to choose the column. By that do you mean it could be "A", or "H", or"XX", etc? That makes it a random column; but how are you wanting to choose the column? Do you want the code to act on every cell in the column or only a certain range in the column?
WillieD24 (rep: 547) Sep 16, '20 at 9:10 pm
Hi WillieD24,
My intent is that the macro will ask " Which Column Do You Wanted Colored?"
And based on my answer, (probably using a variable) it will color that column by value. And the ENTIRE colum will be colored, and each value will have it's own color.
joshie Sep 17, '20 at 6:41 am
Hi Joshie,
This starting to get a bit involved. To do what you want involves creating a user form to get/know the chosen column. There are several tutorials on this site you can look at to learn how to do that. 
There is also another question which needs answering: after a column has been coloured, when the next column is chosen should the last column stay coloured or revert to 'no fill'? Also keep in mind that there are limitations to how the colours will look. If you insert the code above and try it out, you will see that some colours appear the same.Will the values have min/max limits or could they be anything?
WillieD24 (rep: 547) Sep 17, '20 at 7:24 pm
Thanks WillieD24,
I do have some experience with user forms, but i don't think it is necessary here. A simple msgbox, and a variable should be enough. And I really want to keep it simple. Just the one column requested. You do have a point about the choice of colors, because with above code that includes all kinds of colors, the dark ones are not ideal. A roster of light pastel colors would seem what I am looking for.
Anyway, I am working on this myself, and when I have a solution, I will post it.
joshie Sep 21, '20 at 7:15 am
Hi Joshie,
I don't use userforms or msgboxes much so I'm looking forward to see what you come up with.
If there is a range which the cell values could be, maybe you could consider dividing it by 56 and using the colour values 1 to 56 as fill colours. Each cell may not be as unique as you originally hoped but the result would be pleasing to the eye. Just a thought.
WillieD24 (rep: 547) Sep 22, '20 at 8:48 pm
Add to Discussion

Answers

0

Standard procedure would use a function like the one shown below.

Private Function ValueColor(ByVal Value As Variant) As Long

    Dim Values  As Variant
    Dim Colors  As Variant
    Dim Index   As Integer

    Values = Array(0, 1, 2, 3, 4, 5, 6)
    On Error Resume Next
    Index = WorksheetFunction.Match(Value, Values, 0)
    ' if a Value is entered that doesn't exist:-
    ' return the color specified first in the Colors array
    If Err Then Index = 1
    On Error GoTo 0
    ' read cell colours in VBE's Immedaite pane with:-
    ' ? Selection.Font.Color or ? Selection.Interior.Color
    Colors = Array(vbRed, vbYellow, vbGreen, vbBlue, 1137094, 10498160, 49407)
    ValueColor = Colors(Index - 1)
End Function

Here you have two related arrays, Values and Colors. They have the same number of elements. The relationship betwen value and colour is established by the sequence. The value 0  is rserved to indicate that an invalid value was passed to the function causing the function to return the first value in the Colors array. You can add as many values as you like provided you specify the same number of colours.

The function returns a color number (note that enumerations like vbRed are just named numbers). This is a function call you can try. Install it in the same standard code module as the function, place the curser in it and press F5 (=Run).

Sub GetValueColor()
    MsgBox "Colour for Value 5 = " & ValueColor(5)
End Sub

More useful would be the application in an event procedure that responds to a change event. The sample procedure below must be installed in the code module of a worksheet and the procedure ValueColor must then be in the same module. (Read up on "Standard" and "Worksheet" code modules.)

Private Sub Worksheet_Change(ByVal Target As Range)

    Const TriggerRange  As String = "A2:A10"

    ' skip if more than 1 cell was changed (like paste)
    With Target
        If .CountLarge > 1 Then Exit Sub

        If Not Application.Intersect(Target, Range(TriggerRange)) Is Nothing Then
            .Font.Color = ValueColor(Val(.Value))
            ' .Interior.Color = ValueColor(Val(.Value))
        End If
    End With
End Sub

This procedure would fire when a cell in the specified TriggerRange (A2:A10) was changed and change the font colour to one chosen by its value. It only takes a minor change of code to change the cell's fill instead. In fact, the font and fill colours are often set relative to each other but Excel will, automatically, set the font colour to contrast the fill unless you gave over-riding instructions.

Discuss

Discussion

thank-you for your input. In this case I do not want to make any array or other input. Just to be able to select a column and however the amount of values there are, each value will assume a different color. In the event that there are not enough colors, well - start the color roster again.!
joshie Sep 10, '20 at 6:30 am
Thank you for your feedback, Joshie, but you should test the code before you comment on it. You are talking like, "I don't want to start any engine. I just want the car to take me there."
Variatus (rep: 4889) Sep 10, '20 at 8:53 pm
hi, variatus   actaully  i look  for  like  this  code    i try  testing  your  code    but  it gives me error  in this line 
.Font.Color = ValueColor(Val(.Value))

compile error "sub or function not defined"   
in the begining  i thought need this statement 
option explicit 
but  this doesn't success
thanks andvance 
leopard (rep: 88) Sep 12, '20 at 5:08 am
That's because you either didn't install the function ValueColor I provided or you installed it in a code module different from where you placed the Worksheet_Change event procedure. The latter must be installed in the code module of the worksheet on which you want the action and the former on the same sheet.
Variatus (rep: 4889) Sep 12, '20 at 8:44 pm
hi, variatus    but  my  data  in  sheet1   and  the  code  in event sheet1  not else 
leopard (rep: 88) Sep 13, '20 at 6:53 am
Yes. When you make a change in Sheet1 the vent procedure fires. That's because it is in the Sheet1's code module AND because it's name meets the exact requirement. By another name or in another location it won't be found. 
The event procedure calls the function which must, therefore, be placed where that procedure can find it. To keep it simple, I designed it to be in the same module as the event procedure.
Variatus (rep: 4889) Sep 13, '20 at 8:22 pm
thank-you Variatus for the constructive criticism.
joshie Sep 15, '20 at 12:48 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login