Detect the Conditional color of a cell - excel

I'm doing a mail merge from Excel into Word, and I need to copy the background colors of some cells as well as the contents.
Here on SO I've learned that I could add a helper column, and insert a custom function (something like Selection.Interior.Color) that detects the code of the desired cell. I was going to have the mail merge stealthfully pass that code to Word, where a macro would see it and use it to colorize the corresponding table cell.
Unfortunately, Selection.Interior.Color only detects the natural, underlying color of the cell, not the conditionally-formatted color.
Is there a way to detect the color as assigned by the conditional formatting?
(There are 35 different columns using at least 8 different sets of conditional rules).
CONCLUSION: These solutions seem to work, but I decided to avoid adding data or macros to the Excel sheet. Instead I put a macro in Word that basically duplicates the Conditional Formatting functionality. It's slow, but I think its ultimately cleaner.
Thanks, everyone.

As #David said, it seems to be a pain. However, if the conditional formatting includes only the "traditional" standard excel colors (see here), the following seems to respond properly (not exhaustively tested). Column A (rows 1 to 12) contains values from 1 to 12, and conditional formatting were applied to those cells. The code below seems to work, as long as colors are "standard".
Sub Button1_Click()
For i = 1 To 12
Worksheets("Sheet1").Cells(i, 2) = Worksheets("Sheet1").Cells(i, 1).DisplayFormat.Interior.ColorIndex
Next
End Sub

Here is a routine which places the displayformat.interior.color code in a "helper column" next to the column being tested. I also added a column to show the RGB values, but only for interest. Oh, and the colors were all generated by conditional format.
Option Explicit
Sub GetColor()
Dim R As Range, C As Range
Set R = Range(Cells(2, 1), Cells(10, 1))
For Each C In R
C.Offset(0, 1).Value = C.DisplayFormat.Interior.Color
C.Offset(0, 2).Value = converttorgb(C.Offset(0, 1).Value)
Next C
End Sub
Function ConvertToRGB(lColor As Long) As String
Dim H As String
Dim Red As Integer, Green As Integer, Blue As Integer
H = Format(Hex(lColor), "######")
Red = Val("&H" & Right(H, 2))
Green = Val("&H" & Mid(H, 3, 2))
Blue = Val("&H" & Left(H, 2))
ConvertToRGB = Format(Red, "0\, ") & Format(Green, "0\, ") & Format(Blue, "0")
End Function
Depending on how you are transferring this information to Word, you may not even need to have it on the worksheet.

Related

conditional formatting a line based on a value

On this topic
Conditional formating on an entire row based on contents in one column
I used method#2 with the loop. My question is what is the coding I need to add and change to make the cell dark blue, the font white, and bold? (I can live without the bold). Below is my code (text color is red, just to prove to me it worked). The report is a daily download emailed, and I am currently doing conditional formatting every day and need a faster way.
Sub Macro1()
Dim r As Long
For r = 2 To 100
If Cells(r, "C").Value = "Night" Then
Rows(r).Range("b1:p1").Font.Color = vbRed
End If
Next
End Sub

Accessing color value of a conditionally formatted cell (or identifying a conditionally formatted cell) [duplicate]

I would like to obtain the cell background color assigned using a conditional formatting rule in Excel in my VBA script. I realized that using Range.Interior.Color property doesn't have the color resulting of an applied conditional formatting feature from Excel.
I did some research and I found this long way here, it compiles and runs but I don't get the assigned color [I get always (255,255,255)]
I am using Excel 2016 and I am wondering if there is a simpler way to obtain this information using some built-in VBA function or using any other excel trick.
If you want to know the color of a cell that has been colored by a conditional formatting rule (CFR) then use Range.DisplayFormat.Interior.Color¹.
If you want to definitively know what color a cell may or may not have been colored by a CFR you need to iterate through the CFRs that could be affecting that cell and look at each of the Range.FormatConditions(*x*).Interior.Color.
Documentation: range.DisplayFormat
¹ Note: .DisplayFormat is not available for a worksheet UDF.
The code below gives the HEX and RGB value of the range whether formatted using conditional formatting or otherwise. If the range is not formatted using Conditional Formatting and you intend to use iColor function in the Excel as UDF. It won't work. Read the below excerpt from MSDN.
Note that the DisplayFormat property does not work in user defined functions. For example, in a worksheet function that returns the interior color of a cell, if you use a line similar to:
Range.DisplayFormat.Interior.ColorIndex
then the worksheet function executes to return a #VALUE! error.
Public Function iColor(rng As Range, Optional formatType As String) As Variant
'formatType: Hex for #RRGGBB, RGB for (R, G, B) and IDX for VBA Color Index
Dim colorVal As Variant
colorVal = rng.DisplayFormat.Interior.Color
Select Case UCase(formatType)
Case "HEX"
iColor = "#" & Format(Hex(colorVal Mod 256),"00") & _
Format(Hex((colorVal \ 256) Mod 256),"00") & _
Format(Hex((colorVal \ 65536)),"00")
Case "RGB"
iColor = Format((colorVal Mod 256),"00") & ", " & _
Format(((colorVal \ 256) Mod 256),"00") & ", " & _
Format((colorVal \ 65536),"00")
Case "IDX"
iColor = rng.Interior.ColorIndex
Case Else
iColor = colorVal
End Select
End Function
'Example use of the iColor function
Sub Get_Color_Format()
Dim rng As Range
For Each rng In Selection.Cells
rng.Offset(0, 1).Value = iColor(rng, "HEX")
rng.Offset(0, 2).Value = iColor(rng, "RGB")
Next
End Sub
You want Range.DisplayFormat if you need to account for Conditional Formatting
(added in Excel 2010)
.FormatCondition property of range or selection should help with any formatting
Use With...End With
If you want to know the exact color RGB values, u can just try recording macro and get the rgb values.

Automatically coloring partial text within cell in Excel

I'm trying to find a way to let's say input the text "3b" in one cell of a specific range of cells in a specific sheet (let's say cells range: J19:O500 and sheet name:WWP), action which will automatically change that cell's background to blue color and the letter "b" to blue color as well (note that only the "b" letter changes to blue color, the goal is having the letter "b" technically "hidden" in the background color and only visually leaving the "3" text in black color). The numbers in the text will be variable (1, 2, 3, 4, 10, 20, up to 99), but the letters will only be four (b=blue, r=red, g=green, y=yellow). Currently, our teams are manually coloring hundreds of cells every week in a standard file we use, so it would be very valuable if we can make this process automatic by allowing our teams to input the "number" they want for the cell plus a "letter" that will automatically format the cell (b, r, g, y). I understand Macros is an option, but I don't have a lot of experience on that. Thank you very much for your suggestions.
Try this.
Sub setRangeColor()
Dim rngDB As Range, rng As Range
Application.ScreenUpdating = False
Set rngDB = Range("J19:o500")
With rngDB '<~~ Initialize a range
.Font.Color = vbBlack
.Interior.Color = xlNone
End With
For Each rng In rngDB
Select Case Right(rng, 1)
Case "b"
setTextColor rng, vbBlue
Case "r"
setTextColor rng, vbRed
Case "g"
setTextColor rng, vbGreen
Case "y"
setTextColor rng, vbYellow
End Select
Next rng
Application.ScreenUpdating = True
End Sub
Sub setTextColor(rng As Range, myColor As Long)
Dim n As Integer
n = Len(rng)
rng.Characters(n, 1).Font.Color = myColor
rng.Interior.Color = myColor
End Sub
If I am understanding your question correctly you would basically like cells in a selected range to display only a number variable, and then color that cell with the specified letter variable. If you would like to do this without using VBA then you can try this This will require an extra spreadsheet to deal with partial font formatting
This can be done like so:
Create a new sheet in your workbook.
Type the following formula in the same cell your real sheet starts with this color formatting. Ex) range is in Sheet2 A1:B3, put this formula in Sheet3 A1:B10
Formula--> =IFERROR(MID(Sheet2!A1,1,LEN(Sheet2!A1)-1),"")
*This will return only the numbers in the cells from the other spreadsheet, assuming no other characters proceed after r,g,b,y
Make sure to copy this formula to the entire range of cells you wish to format
Select the range in sheet 3 that will be worked with (same range in sheet 2)
Go to conditional formatting
Select New rule
6.Type in this formula
=RIGHT(Sheet2!A1:B3,1)="R"
(A1:B3 happens to be my example range. R is for red
Choose red formatting
Repeat Steps 3 to 7 until each color format is satisfied.
(simply change the 'R' in step 6's formula to G,B,or Y and change the formatting respectively.

Excel, make sum across pages based on cell color

Maybe this is a trivial question but just in case anyone encountered the same problem.
I have a excel workbook with several sheets and in each sheet, I want to mark some cells with a particular color (green) and have them sum all together (from all sheets).
This is a spent personal report and each sheet contains comments, so I don't have precise locations for sub-totals.
One solution could be sheet1:A20 + sheet2:B34 ... and so on (manually)
Other it cross my mind will be for each cell that I want as sub-total to colorize in green and make a VBA to sum all across all sheets by color?
Appreciate ideas,
You could do something like this to sum up all values in a specific Range (B2-B4 in my case - but that could be expanded to other ranges, of course):
Dim sumYellowCells As Integer
Dim sumGreenCells As Integer
For Each cell In ActiveSheet.Range("B2:B4")
If cell.Interior.Color = vbGreen Then
sumGreenCells = sumGreenCells + cell.Value
End If
If cell.Interior.Color = vbYellow Then
sumYellowCells = sumYellowCells + cell.Value
End If
Next
Debug.Print sumGreenCells & "-" & sumYellowCells

Excel Conditional Formatting vs. a Reference Table

I'm trying to determine how to format a column of values against a reference table. If this is possible, I feel like it would require use of the "forumula" conditional formatting style; however, I haven't had much experience with that, and I haven't been able to find exactly what I'm looking for--not sure I'm correctly stating my problem.
The intent is to be able to "Red/Green" the cells in the Column B depending on which "Level" is indicated in Cell B1: if the value in column B is greater than or equal to the value in the reference chart for that Level, the value in column B should turn green, otherwise it should turn red.
I have hundreds of columns of information like this, all of which needs to be formatted in the same manner. This information will also be regularly updated ("Level" value in B1, "1/2/3" values in column B).
I'm using Excel 2016, if that is relevant information.
Any help would be greatly appreciated--I can provide additional information if necessary.
made a really basic VBA script for this if you just want to use that. code is below.
Public Sub UpdateTheLevel()
Dim Currentlevel, templevel As String
Dim i, j, MaxLevel, column, MaxSkill, currlevel, wantedlevel As Integer
column = 0
''Need to set MaxLevel, MaxSkill
Currentlevel = ActiveSheet.Cells(2, 1).Value
For i = 4 To MaxLevel
templevel = ActiveSheet.Cells(2, i).Value
If templevel = Currentlevel Then
column = i
Exit For
Else
Next i
If column = 0 Then
MsgBox "Level Not Found."
Else
For j = 3 To MaxSkill
currlevel = Cells(j, 2).Value
wantedlevel = Cells(j, column).Value
If currlevel < wantedlevel Then
Range (Cells(j, 2)), Interior.ColorIndex = 3
Else
Range (Cells(j, 2)), Interior.ColorIndex = 4
Next j
End Sub
You could tie this into a form to run from a button press or whenever the file is opened.
This can be done without VBA, using two conditional formatting rules that use formulas to determine the format.
Select B3 to B17, taking great care that B3 is the active cell. Then create two rules.
Formula for red:
=AND(ISNUMBER(B3),B3<INDEX($D$3:$H$17,ROW(B3)-2,MATCH($B$1,$D$2:$H$2,0)))
Formula for green:
=AND(ISNUMBER(B3),B3>=INDEX($D$3:$H$17,ROW(B3)-2,MATCH($B$1,$D$2:$H$2,0)))

Resources