How do I set the conditional formatting on 2 different columns of data if column(L) <> Column(H) using VBA. The format I would like consist of Font:Red, BackFill: Yellow, for the entire row.
This is what I have so far.
With Range("H:H,L:L")
.FormatConditions.Delete
.FormatConditions.Add Type:=xlExpression, Formula1:="=$H1<>$L1"
With Range("H:H,L:L").FormatConditions(1).Font
.Color = -16776961
.TintAndShade = 0
End With
End With
No error and Thanks for your time, however, I found the solution elsewhere.
With Rows("1:1048576")
.FormatConditions.Delete
.FormatConditions.Add Type:=xlExpression, Formula1:="=AND(ROW()<>1,$H1<>$L1)"
With Rows("1:1048576").FormatConditions(1).Font
.Bold = True
.Color = -16776961
End With
With Rows("1:1048576").FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 65535
End With
End With
Related
I have recorded a macro that corrects the conditional formatting rules of a table every time they get messed up (because of adding or removing lines I suppose…)
And I put a button to activate the macro in the sheet
I need to replicate the same table in several sheets (increasing number of sheets) and I want my macro to function on all of them (not necessarily simultaneously) in addition of having this common table, most of the sheets have other tables also, but there will be 1 table that will be replicated in MOST sheets.
(Basically create a template sheet containing the table and macro button that users will replicate for each new client
Since the tables will have same number of columns and column titles, is it possible to tweak it so it works on any table where the cursor has selected a cell? Or similar?
Maybe some way of changing the ref from “bookingInfo” to “selected table”
FYI: I do not know how to write VBA at all
Here is the code I have:
Application.ScreenUpdating = False
Application.Goto Reference:="BookingInfo"
Selection.ListObject.Range.FormatConditions.Delete
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=$B4<>$B5"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Bold = True
.Italic = False
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=$A5<>"""""
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.Pattern = xlLightDown
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.14996795556505
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=$AN5=""Full PMT"""
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent3
.TintAndShade = 0.399945066682943
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=$AN5=""Partial PMT"""
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent6
.TintAndShade = 0.399945066682943
End With
Selection.FormatConditions(1).StopIfTrue = False
Application.ScreenUpdating = True
End Sub
Any Help?
You could do it like this:
Sub CFUpdate()
Dim lo As ListObject, rng As Range
Set lo = Selection.ListObject
If lo Is Nothing Then 'is the selection in a listobject?
MsgBox "First select any cell in the Table to be updated", vbExclamation
Exit Sub 'nothing to do...
End If
Set rng = lo.DataBodyRange 'range to be formatted
rng.FormatConditions.Delete
With AddFC(rng, xlExpression, "=$B4<>$B5").Font
.Bold = True
.Italic = False
.TintAndShade = 0
End With
With AddFC(rng, xlExpression, "=$A5<>""""").Interior
.Pattern = xlLightDown
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.14996795556505
End With
With AddFC(rng, xlExpression, "=$AN5=""Full PMT""").Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent3
.TintAndShade = 0.399945066682943
End With
With AddFC(rng, xlExpression, "=$AN5=""Partial PMT""").Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent6
.TintAndShade = 0.399945066682943
End With
Application.ScreenUpdating = True
End Sub
'factoring out some common steps
Function AddFC(rng As Range, fcType As XlFormatConditionType, frmla As String)
Dim fc As FormatCondition
Set fc = rng.FormatConditions.Add(Type:=fcType, Formula1:=frmla)
fc.StopIfTrue = False
Set AddFC = fc 'return the FormatCondition we just added
End Function
Pulled some of the common code out into a separate function to reduce the bulk of the code when adding each format condition.
Note you'll also need to adjust the formulas if the tables don't all start on the same row...
I have a large table spanning from D5 to AM39. Each column has its average value in cell D40, E40, F40, etc. I want to format the cells so that if the number in that column is higher than the average, color green and if lower color red.
I am extremely new to VBA but have this script thus far that is supposed to color cells greater than average but does not work (I think it has something to do with Cells(4,39) index being wrong, but am not sure.
Application.CutCopyMode = False
With Range(Cells(5, 39), Cells(4, 39))
.FormatConditions.Delete
.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
Formula1:="=$D40"
.FormatConditions(1).Interior.color = RGB(0, 150, 0)
End With
End Sub
Appreciate any tips
EDIT********
Using the record macro feature I believe I have a closer solution to what I am looking for, however, the formatting doesn't align with the averages per row (cells are red that should be green, and vice versa)
With Range(Cells(39, 4), Cells(5, 39)).Select
Application.CutCopyMode = False
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
Formula1:="=D$40"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.color = -16752384
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.color = 13561798
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Application.CutCopyMode = False
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _
Formula1:="=D$40"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.color = -16383844
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.color = 13551615
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
End With
End Sub
Try this (using the built-in "compare to average" CF feature)
Sub AddCF()
Dim rngData As Range, col As Range
Set rngData = ActiveSheet.Range("D5:AM39") 'your table data
Application.ScreenUpdating = False
For Each col In rngData.Columns 'for each column in the data range
With col.FormatConditions.AddAboveAverage 'for >Avg
.AboveBelow = xlAboveAverage
.Interior.Color = vbRed
End With
With col.FormatConditions.AddAboveAverage 'for <Avg
.AboveBelow = xlBelowAverage
.Interior.Color = 5296274
End With
Next col
End Sub
If you want to use your existing average formulas:
Sub AddCF2()
Dim rngData As Range, col As Range, addr
Set rngData = ActiveSheet.Range("D5:AM39")
Application.ScreenUpdating = False
For Each col In rngData.Columns 'for each column in the data range
'absolute row, relative column address
addr = col.Cells(col.Cells.Count).Offset(1).Address(True, False) 'avg cell address
With col.FormatConditions
With .Add(Type:=xlCellValue, Operator:=xlGreater, Formula1:="=" & addr)
.Font.Color = -16383844
.Interior.Color = 13551615
End With
With .Add(Type:=xlCellValue, Operator:=xlLess, Formula1:="=" & addr)
.Font.Color = -16752384
.Interior.Color = 13561798
End With
End With
Next col
End Sub
I'm trying to turn any cell that matches a preset value to a color.
If cells values are:
S-DAYS, C-DAYS, DAYS it will turn the cell BLUE with black text
E SWING, S-E SWING, C-E SWING it will turn Green with black text
L SWING, S-L SWING, C-L SWING it will turn Light Purple with black text
LATES, S-LATES, C-LATES it will turn Gray with black text
AOT will turn Yellow with black text
VAC, OUT, MIL, TRAIN it will turn the cell BLACK with White Text
I recorded the following. How do I to make it apply automatically to the sheet without needing to be prompted?
Sub DAConditionalFormating()
'
' DAConditionalFormating Macro
'
'
Range("D14:XFD999").Select
Selection.FormatConditions.Add Type:=xlTextString, String:="DAYS", _
TextOperator:=xlContains
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 15773696
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.FormatConditions.Add Type:=xlTextString, String:="E SWING", _
TextOperator:=xlContains
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 5296274
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.FormatConditions.Add Type:=xlTextString, String:="LATES", _
TextOperator:=xlContains
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.249946592608417
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.FormatConditions.Add Type:=xlTextString, String:="VAC", _
TextOperator:=xlContains
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
End Sub
Applying conditional formatting to a whole sheet is a really, really bad idea. The calculation will kick in every time you edit ANY cell in sheet.
Instead of conditional formatting the whole sheet, use code to format only the cell that was just changed. Format the cell fill and the font.
Run this code in a Worksheet_Change event in the Sheet module and let it work on the target cell. Then it will run fast and change only the cell that was just changed.
Something like this. Add more ElseIF as required. The code goes into the Sheet module of the sheet.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Value = "foo" Then
With Target.Interior
.PatternColorIndex = xlAutomatic
.Color = 15773696
.TintAndShade = 0
End With
ElseIf Target.Value = "bar" Then
With Target.Interior
.PatternColorIndex = xlAutomatic
.Color = 5296274
.TintAndShade = 0
End With
End If
End Sub
I have the below code: this only fills rows in column a, I would like all columns to be filled.
Sheets("Schedule").Cells.Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=m1=""Part"""
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent1
.TintAndShade = 0.399945066682943
End With
I have made it select all cells to get around this but it hasn't worked
Your formula in
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=M1=""Part"""
is not correct. Just use
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=$M1=""Part"""
instead and it will work.
Note:
The difference here is the $ infront of the M which assures that the column is fixed but the row 1 is not and can iterate.
And I recommend not to use .Select and Selection. at all (this is bad practices and they should always be avoided).
Your code can be improved like this:
With Sheets("Schedule").Cells
.FormatConditions.Add Type:=xlExpression, Formula1:="=$M1=""Part"""
.FormatConditions(.FormatConditions.Count).SetFirstPriority
With .FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent1
.TintAndShade = 0.399945066682943
End With
End With
If you want all cells in sheet to turn blue when they have word "Part". then you should use this code.
Sheets("Schedule").Cells.Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=A1=""Part"""
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent1
.TintAndShade = 0.399945066682943
End With
in case you want conditional to start at "M" column, you should use this line Sheets("Schedule").range("M:AAA").select instead of Sheets("Schedule").Cells.Select.
I'm doing conditional formatting in a macro (because I'm constantly applying it along with other formatting repeatedly to a fresh, raw export).
Objective: highlight any row where the text in cell J(n) is "No Activity"
Currently using:
With Cells
.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=($J1=""No Activity"")"
With .FormatConditions(.FormatConditions.Count)
.SetFirstPriority
With .Interior
.PatternColorIndex = xlAutomatic
.Color = 7405514
.TintAndShade = 0
End With
StopIfTrue = False
End With
End With
...which works great. The above was cleaned up using a google search and a recording that originally gave me:
Cells.Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=($N1=""No Activity"")"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent4
.TintAndShade = 0.599963377788629
End With
Selection.FormatConditions(1).StopIfTrue = False
So I was feeling all proud and accomplished... but I also want to highlight rows (in a different color) where the cell in Column J (per above) contains "Quote" at any point in the text of the cell.
When I recorded a macro of doing it as conditional formatting, it didn't really clarify anything for me: (ok, it made it worse)
Selection.FormatConditions.Add Type:=xlTextString, String:="Quote", _
TextOperator:=xlContains
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent1
.TintAndShade = 0.399945066682943
End With
Selection.FormatConditions(1).StopIfTrue = False
I'm just not catching how it should change in
Type:=xlExpression, Formula1:= _
"=($J1=""No Activity"")"
All ideas greatly appreciated!
This works in Excel 2010:
With Cells
.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=($J1=""No Activity"")"
With .FormatConditions(.FormatConditions.Count)
.SetFirstPriority
With .Interior
.PatternColorIndex = xlAutomatic
.Color = 7405514
.TintAndShade = 0
End With
StopIfTrue = False
End With
.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=ISNUMBER(SEARCH(""*quote*"",$J1))"
With .FormatConditions(.FormatConditions.Count)
.SetFirstPriority
With .Interior
.PatternColorIndex = xlAutomatic
.Color = 4405514
.TintAndShade = 0
End With
StopIfTrue = False
End With
End With
Obviously you'd need to change the Color for the 2nd FormatConditions.Add section.
Edit: Realized you were looking for "Quote" anywhere in the cell, so I've updated the code from my original posting.