Assign a value to a range of cells based on the color of conditional formatting

JakeP

New Member
Joined
Apr 23, 2023
Messages
14
Office Version
  1. 2019
Platform
  1. Windows
Hello all color formatting experts!

I have searched all threads and much online without much luck.

I have a rather complex sheet that automatically formats the color of a range of cells based on a date range in other cells. (creates a Gantt Chart)
If that conditional color is green for example, I'd like to auto assign a numerical value to each of those green cells. (These would be for hours assigned for that day to ultimately create a daily team workload capacity analaysis)
I'd like this to work on an entire desired range of cells and also have the ability to do this for different colors. (if yellow, assign a different numerical value, blue...)

I'd also like this to work on any colors whether they are manually assigned by normal formatting means or by conditional methods.


I assume VBA would be required, considering this would need to evaluate conditional formatting?

Hopefully, this makes sense.

Thank you very much in advance for any support!

JP
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
This could be done with formulas if you were just concerned about conditional formatting; you could just use the same conditions used in conditional formatting. But for manual formatting, you will probably need VBA. (There may be some obscure macro available for Excel 4.0 macros that can detect fill color, but I've never used it. That could go into a named formula without using VBA.)

In any case, your question is pretty conceptual with no concrete details. To give you actual VBA code to do this we would need know the RGB numbers for each of the colors you are using, and the corresponding values you want to assign, and the applicable cell ranges.
 
Upvote 0
Hi Jeff,
Thanks for the quick reply.

The RGB values and variables can be anything for the VBA example code.
I think I can modify the code to match my own colors. (green= 00,255,00...) and change the corresponding range(s) and values assigned for each color.
Would this be possible?

1695573745411.png


Thanks, JP
 
Upvote 0
I don't know how you want to use it so here is the code to do it and two ways to call it. I just used Red, Green, and Blue, and values 1, 2, and 3. Modify to suit. You use this one sub for the entire range of cells on the sheet you want to check.

VBA Code:
' Call from code for a specific range
Public Sub SetValuesForColor(R As Range)

   Dim Cell As Range
  
   For Each Cell In R
  
      Select Case Cell.DisplayFormat.Interior.Color
     
         Case RGB(255, 0, 0)
            Cell.Value = 1
           
         Case RGB(0, 255, 0)
            Cell.Value = 2
           
         Case RGB(0, 0, 255)
            Cell.Value = 3
           
      End Select
  
   Next Cell

End Sub

' To call from user interface for current Selection
Public Sub SetValuesForColorSelection()
   SetValuesForColor R:=Selection
End Sub

' To call from code
Public Sub InvokeSetValues()
   SetValuesForColor R:=Worksheets("Color Test").Range("A1:D4")
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,703
Messages
6,173,973
Members
452,540
Latest member
haasro02

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