conditional formatting a line based on a value - excel

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

Related

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

Detect the Conditional color of a cell

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.

Automatically copy a cells colour to another cell

This is a very simple problem that I can't seem to find the solution. Basically I use 3 different cell styles (good,neutral and bad). What I simply want to do is I want the cell adjacent to the one I colour coded to be the same colour. For example cell O11 I selected it to be good (green colour), hence cell M11 should automatically change its cell style according to cell O11.
Any suggestions?
P.S O11 will be set manually (no conditional formatting)
To solve your problem you need to create a variable to hold the cells color value and set that value back to another cell. Use the following example:`To solve your problem you need to create a variable to hold the cells color value and set that value back to another cell. Use the following example:
To solve your problem you need to create a variable to hold the cells color value and set that value back to another cell. Use the following example:
Sub Copy_Color()
Dim iColor As Long
iColor = ActiveCell.Interior.Color
ActiveCell.Offset(0, 1).Interior.Color = iColor
End Sub
To solve your problem you need to create a variable to hold the cells color value and set that value back to another cell. Use the following example:
Sub Copy_Color()
Dim iColor As Long
Dim i as long
for i = 11 to 20
iColor = worksheets("Sheet name").range("M" & i).Interior.Color
worksheets("Sheet name").range("O" & i).Interior.Color = iColor
next
End Sub

Macro script to Apply to Button for Formatting in Excel 2010

I've been searching the world over and I cannot create nor find a macro for a very basic function that I need. All I want to do is to be able to compare column A with column B and if they do not match, highlight red. I need this in a macro because I want to assign it to a button. I can do it with conditional formatting but not in VB. Can anyone provide some insight?
Typically, data requiring this type of action would have column header labels and I will assume that.
Sub red_dupes()
Dim a As Range
With Sheets("Sheet1").Cells(1, 1).CurrentRegion
With .Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count)
.Columns("A:B").Cells.Interior.ColorIndex = xlAutomatic
For Each a In .Columns(1).Cells
If a.Value2 <> a.Offset(0, 1).Value2 Then
a.Resize(1, 2).Interior.ColorIndex = 3
End If
Next a
End With
End With
End Sub
This particular sub removes all previous colour Fill before looping through the cells in column A and marking all A→B non-matches with a red color fill. Do NOT use if you want to retain existing color fills.

Resources