Here's my problem. There are two conditional formatting sections in the code below. The first one highlights the cells red, and the second highlights the cells orange. When I run this code, it highlights the second conditional formatting yellow and does not apply any fill to the first conditional formatting. When I watch code run line by line, the first section appears to work but then it change the fill to orange in the first conditional formatting section and it doesn't apply any fill to the second section.
Sub ConditionalFormat()
Dim wb As Workbook
Dim ws As Worksheet
Dim lRow As Long
Set wb = ThisWorkbook
Set ws = wb.Sheets("CSR")
lRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
'
Application.ScreenUpdating = False
With Range("N10:AN" & lRow)
.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=AND(N10>=($AR10-7),N10>0)"
.FormatConditions(Range("N10:AN" & lRow).FormatConditions.Count).SetFirstPriority
With Range("N10:AN" & lRow).FormatConditions(1).Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 13551615
.TintAndShade = 0
End With
End With
Range("N10:AN" & lRow).FormatConditions(1).StopIfTrue = False
With Range("V10:V" & lRow)
.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=AND($N10<=($V10-35),N10>0,$C10>0)"
'.FormatConditions(Range("V10:V" & lRow).FormatConditions.Count).SetFirstPriority
With Range("V10:V" & lRow).FormatConditions(1).Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 10284031
.TintAndShade = 0
End With
Range("V10:V" & lRow).FormatConditions(1).StopIfTrue = False
End With
Application.ScreenUpdating = True
End Sub`
Not a real answer but wanted to suggest you simplify your code so you can concentrate on the actual issue a bit more easily:
Sub ConditionalFormat()
Dim wb As Workbook
Dim ws As Worksheet
Dim lRow As Long
Set wb = ThisWorkbook
Set ws = wb.Sheets("CSR")
lRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
AddFormulaCF ws.Range("N10:AN" & lRow), _
"=AND(N10>=($AR10-7),N10>0)", _
13551615
AddFormulaCF ws.Range("N10:AN" & lRow), _
"=AND($N10<=($V10-35),N10>0,$C10>0)", _
10284031
End Sub
'Apply a formula-based CF fill to a range
Sub AddFormulaCF(rng As Range, frm As String, clr As Long)
' `.Add()` returns the added FormatCondition, so you can work directly with that.
With rng.FormatConditions.Add(Type:=xlExpression, Formula1:=frm)
.SetFirstPriority
.StopIfTrue = False
With .Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = clr
.TintAndShade = 0
End With
End With
End Sub
Related
I want to clear and reset the conditional formatting rules for my workbook. There are groups of values that I want conditionally formatted to show as Green, Yellow, Orange, Red, and Pink. The values for each color group are stored in an array.
Below is my code so far, but only showing the for loops for green and yellow. I get the error on this line Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual,Formula1:=item
Sub ResetFormat()
Dim ws As Worksheet
Dim item As Variant
Dim arrGreen As Variant
arrGreen = Array(Worksheets("Drop down").Range("N11:N14"))
Dim arrYellow As Variant
arrYellow = Array(Worksheets("Drop down").Range("O11:O13"))
Dim arrOrange As Variant
arrOrange = Array(Worksheets("Drop down").Range("P11:P14"))
Dim arrRed As Variant
arrRed = Array(Worksheets("Drop down").Range("Q11:Q14"))
Dim arrPink As Variant
arrPink = Array(Worksheets("Drop down").Range("R11:R12"))
For Each ws In Sheets
Cells.Select
Selection.Cells.FormatConditions.Delete
For Each item In arrGreen
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
Formula1:=item
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 5287936
.TintAndShade = 0
End With
Next item
For Each item In arrYellow
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
Formula1:=item
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 49407
.TintAndShade = 0
End With
Next item
Next ws
End Sub
What worked for me was using a range instead of an array, and adding the following line:
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
The resulting code:
Sub ResetFormat()
Dim ws As Worksheet
Dim item As Variant
Dim rngGreen As Range
Set rngGreen = Worksheets("Drop down").Range("N11:N14")
Dim arrYellow As Range
Set rngYellow = Worksheets("Drop down").Range("O11:O13")
Dim rngOrange As Range
Set rngOrange = Worksheets("Drop down").Range("P11:P14")
Dim rngRed As Range
Set rngRed = Worksheets("Drop down").Range("Q11:Q14")
Dim rngPink As Range
Set rngPink = Worksheets("Drop down").Range("R11:R12")
For Each ws In Sheets
ws.Activate
Cells.Select
Selection.Cells.FormatConditions.Delete
For Each item In rngGreen
Cells.Select
Selection.FormatConditions.Add Type:=xlTextString, String:=item, TextOperator:=xlEqual
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.Color = 5287936
End With
Selection.FormatConditions(1).StopIfTrue = False
Next item
For Each item In rngYellow
Cells.Select
Selection.FormatConditions.Add Type:=xlTextString, String:=item, TextOperator:=xlEqual
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.Color = 49407
End With
Selection.FormatConditions(1).StopIfTrue = False
Next item
Next ws
End Sub
I have a macro that makes a formatting rule of multiple cell areas and must paint the cell yellow if it contains "S" and it works. But I would also like the cell to the right of the cell that contains "S" painted yellow, but only one cell to the right - not the whole row, is that possible? I imagine it's going to take place inside the "WITH statement, but I can not really move on
Sub Makro2()
Range("D6:E30,G6:H30,J6:K30,M6:N30,P6:Q30").Select
Range("P6").Activate
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
Formula1:="=""S"""
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
End Sub
I tried explaining in a comment the involvements of trying to conditional format of a discontinuous range. For the cell really processed by your shown code, you can accomplish what you need using the next code. The basis of conditional formatting behavior is that it formats only the cell where the conditional format belongs:
Sub Makro2Bis()
Dim rng As Range, offrng As Range
Set rng = Range("P6"): Set offrng = rng.Offset(0, 1)
With rng
.FormatConditions.Delete
.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:="=""S"""
.FormatConditions(1).SetFirstPriority
With .FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
End With
.FormatConditions(1).StopIfTrue = False
End With
With offrng
.FormatConditions.Add Type:=xlExpression, Formula1:="=" & rng.Address(0, 0) & "= ""S"""
.FormatConditions(1).Interior.Color = rng.FormatConditions(1).Interior.Color
.FormatConditions(1).StopIfTrue = False
End With
End Sub
Please, test it and send some feedback.
Edited:
Please, test the (more complicated) version creating the conditional formatting for the discontinue range, in a way you asked for: The right neighbour cell will have the interior yellow, too:
Sub Makro3Bis()
Dim rng As Range, arr, rng1 As Range, rng2 As Range, rng3 As Range
Set rng = Range("D6:E30,G6:H30,J6:K30,M6:N30,P6:Q30")
arr = buildThreeRngs(rng)
Set rng1 = arr(0) 'the first column of the discontinuous range areas
Set rng2 = arr(1) 'the second column of the discontinuous range areas
Set rng3 = arr(2) 'the next column after the discontinuous range areas
With rng1
.FormatConditions.Delete
.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:="=""S"""
.FormatConditions(1).SetFirstPriority
With .FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
End With
.FormatConditions(1).StopIfTrue = False
End With
With rng2 'it will have two conditions. The second one relative to its left neighbour cell.
.FormatConditions.Delete
.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:="=""S"""
.FormatConditions(1).SetFirstPriority
With .FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
End With
.FormatConditions(1).StopIfTrue = False
.FormatConditions.Add Type:=xlExpression, Formula1:="=" & rng1.cells(1).Address(0, 0) & "= ""S"""
.FormatConditions(2).Interior.Color = rng1.FormatConditions(1).Interior.Color
.FormatConditions(2).StopIfTrue = False
End With
With rng3
.FormatConditions.Delete
.FormatConditions.Add Type:=xlExpression, Formula1:="=" & rng2.cells(1).Address(0, 0) & "= ""S"""
.FormatConditions(1).Interior.Color = rng.FormatConditions(1).Interior.Color
.FormatConditions(1).StopIfTrue = False
End With
End Sub
Please, test it and send some feedback.
Second Edit:
Looking only to your picture and not basing on the code you posted, probably, the my first code (for one cell) adapted in the next way should be what you need:
Sub Makro2BisBis()
Dim rng As Range, offrng As Range
Set rng = Range("D6:D30,G6:G30,J6:J30,M6:M30,P6:P30")
Set offrng = rng.Offset(0, 1)
Debug.Print offrng.Address: Stop
With rng
.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:="=""S"""
.FormatConditions(1).SetFirstPriority
With .FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
End With
.FormatConditions(1).StopIfTrue = False
End With
With offrng
.FormatConditions.Add Type:=xlExpression, Formula1:="=" & rng.cells(1).Address(0, 0) & "= ""S"""
.FormatConditions(1).Interior.Color = rng.FormatConditions(1).Interior.Color
.FormatConditions(1).StopIfTrue = False
End With
End Sub
It will make yellow the cells in the range you have in your code, but using only each area first column...
Please, test it and send some feedback.
Conditional Formatting in VBA
For the second part in Excel, you would use the following Conditional Formatting formula:
=D6="s"
The Code
Option Explicit
Sub Makro2()
Const ColOffset As Long = 1
Const Criteria As String = "s"
Dim rg As Range: Set rg = Range("D6:D30,G6:G30,J6:J30,M6:M30,P6:P30")
' xlCellValue
With rg
.ClearFormats
.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
Formula1:="=""" & Criteria & """"
With .FormatConditions(.FormatConditions.Count)
.SetFirstPriority
With .Interior
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
End With
.StopIfTrue = False
End With
End With
' xlExpression
With rg.Offset(, ColOffset)
.ClearFormats
.FormatConditions.Add Type:=xlExpression, _
Formula1:="=" & .Cells(1).Offset(, -ColOffset).Address(0, 0) _
& "=""" & Criteria & """"
With .FormatConditions(.FormatConditions.Count)
.SetFirstPriority
With .Interior
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
End With
.StopIfTrue = False
End With
End With
End Sub
I am working on a macro that copies, pastes, and then creates templates forms of various sizes. Before the macro saves the template sheet as a separate file, I have it searching through a range - typically through D14:G end of data range - and highlights blank cells in a custom color. However, I have one very specific use case where there are no blank cells within the range (D14:G16), so it has been selecting all blank cells below this range (A17 to end of sheet). Can anyone help me work past this? Below is the excerpt from the macro that highlights the blankcells:
Set rLastCell = Sheets("Diversity Form").Cells.find(What:="*", After:=Sheets("Diversity Form").Cells(1, 1), LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False)
'ColumnLetter2 = Split(Cells(1, rLastCell.Column).Address, "$")(1)
lCol = Sheets("Diversity Form").Cells(Rows.count, 4).End(xlUp).Row
'Dim ColumnLetter As String
'color only blank cells
For h = 4 To 7
ColumnLetter = Split(Cells(1, h).Address, "$")(1)
Let item = ColumnLetter & "14:G" & lCol
Sheets("Diversity Form").Range(item).SpecialCells(xlCellTypeBlanks).Select
On Error Resume Next
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent1
.TintAndShade = 0.599993896298105
.PatternTintAndShade = 0
End With
Next
No need to loop here, or to mess with column letters, or to Select.
Use WorksheetFunction.CountBlank first to test if there are any blanks.
With Sheets("Diversity Form")
Dim lastRow As Long
lastRow = .Cells(.Rows.Count, 4).End(xlUp).Row
Dim checkRange As Range
Set checkRange = .Range(.Cells(14, 4), .Cells(lastRow, 7)) ' Or .Range("D14:G" & LastRow)
End With
If WorksheetFunction.CountBlank(checkRange) > 0 Then
With checkRange.SpecialCells(xlCellTypeBlanks).Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent1
.TintAndShade = 0.599993896298105
.PatternTintAndShade = 0
End With
End If
Trying to get conditional formatting running through VBA code (since values change every day, also have a code to clean that deletes the Conditional formatting I did, non-VBA).
Want to highlight values on column P that are numeric > 0 and O4 > 0.
Sheets("RAW DATA FILE").Cells("A1").Select
Sheets("RAW DATA FILE").Columns("A:A").EntireColumn.AutoFit
Sheets("RAW DATA FILE").Range("P4").Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=AND(ISNUMBER($P4), $P4>0, $O4>0)"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 49407
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
The first line triggers the error:
Run-time error '5'
Invalid procedure call or argument
Would appear there is an issue with your first line, should be Range instead of Cells:
Sub Conditional_formatting_2_conditions_met()
Sheets("RAW DATA FILE").Activate
Sheets("RAW DATA FILE").Range("A1").Select
Sheets("RAW DATA FILE").Columns("A:A").EntireColumn.AutoFit
Sheets("RAW DATA FILE").Range("$P:$P").Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=AND(ISNUMBER($P1), $P1>0, $O1>0)"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 49407
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
End Sub
You may use the loop throug all cells in range if You need to update more cells. You define the range at With .Range("P1:P10") line
Sub Conditional_formatting_2_conditions_met()
Dim lRow As Long
Dim cel As Range
With Sheets("RAW DATA FILE")
.Columns("A:A").EntireColumn.AutoFit
With .Range("P1:P10")
For Each cel In .Cells
With cel
lRow = lRow + 1
With .FormatConditions.Add(Type:=xlExpression, Formula1:="=AND(ISNUMBER($P" & CStr(lRow) & "), $P" & CStr(lRow) & ">0, $O" & CStr(lRow) & ">0)")
.SetFirstPriority
.StopIfTrue = False
With .Interior
.PatternColorIndex = xlAutomatic
.Color = 49407
.TintAndShade = 0
End With
End With
End With
Next cel
End With
End With
End Sub
Hello again the last time I posted a question it was resolved quite quickly.
Essentially what I am trying to do is if a row in column r says Project Complete then I want it to grey out the row from column D:BM
I was playing around with a few things but it doesn’t want to work.
The column r is also a vlookup formula to another workbook, not sure if that makes a difference.
Dim rng As Range, cell As Range
Set rng = Range("R10:R1000")
For Each cell In rng
If cell.Value = "Project Complete" Then
Range("D" & ActiveCell.Row & ":BM" & ActiveCell.Row).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.149998474074526
.PatternTintAndShade = 0
End With
End If
Next cell
You can loop through the range without selection:
Sub OhYa()
Dim rng As Range, c As Range
Set rng = Range("R10:R1000")
For Each c In rng.Cells
If c = "Project Complete" Then
With Range("D" & c.Row & ":BM" & c.Row).Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.149998474074526
.PatternTintAndShade = 0
End With
End If
Next c
End Sub