I wrote down below code:
Sub formatCondMesi()
Worksheets(1).Unprotect Password:="ponzio"
Dim i As Integer
'For i = 2 To 13
Worksheets(1).Select
Worksheets(1).Cells.FormatConditions.Delete
'1)
With Worksheets(1).Application.Union(Range("C7:O149"), Range("C155:O297"), Range("C303:O445"), Range("C451:O593"))
.FormatConditions.Add Type:=xlExpression, Formula1:="=$H7<0"
.FormatConditions(1).Font.ColorIndex = 3
End With
'2)
With Worksheets(1).Application.Union(Range("J150"), Range("L150"), Range("N150:O150"))
.FormatConditions.Add Type:=xlExpression, Formula1:="=$E150="""""
.FormatConditions(1).Font.ColorIndex = 2
End With
'3)
i = 1
If i = 1 Then
A = Worksheets(1).Range("F4").Value
B = Worksheets(1).Range("F5").Value
End If
With Worksheets(1).Application.Union(Range("E7:E149"), Range("E155:E297"), Range("E303:E445"), Range("E451:E593"))
.FormatConditions.Add Type:=xlExpression, Formula1:="=OR($E7<" & A & ";$E7>" & B & ")" '43101 43131
'.FormatConditions(1).SetFirstPriority = True
.FormatConditions(1).Font.ColorIndex = 3
.FormatConditions(1).Font.Bold = True
.FormatConditions(1).Font.Strikethrough = True
End With
'4)
With Worksheets(1).Application.Union(Range("O7:O149"), Range("O447:O593"), Range("O299:O445"), Range("O151:O297"))
.FormatConditions.Add Type:=xlExpression, Formula1:="=$O7=0"
.FormatConditions(1).Font.ColorIndex = 3
End With
'5)
With Worksheets(1).Application.Union(Range("L7:L149"), Range("L151:L297"), Range("L299:L445"), Range("L447:L593"))
.FormatConditions.Add Type:=xlExpression, Formula1:="=ISNA($L7)"
.FormatConditions(1).Font.ColorIndex = 2
End With
End Sub
I have two issues:
when I reach CF n. 3 ('3) it wrongly apply formats, the strike-through line is applied in another rule and bold style too..and i can't understand with which criteria it is selecting other CF
when I reach CF n.4 it returns:
1004 error, object not defined
For issue 1), probably you have to add
.FormatConditions(1).StopIfTrue = True
else, all conditional formatting rules will be checked and applied
For issue 2): Do you work by any chance with Excel 2003? There, the max. number of rules was limited to 3, so maybe that's your issue. Else I have no idea, I made a quick test and could add more than 3 rules without problem.
Related
I am trying to create 2 conditional formats on a spreadsheet I am manipulating using MS Access 365 VBA. I have created the following code to do this:
Set lrngReport = .Range("$A$2:$" & lstrLastCol & "$" & lintLastRow)
With lrngReport
.FormatConditions.Delete
'Condition where no record in Donor Comments Modified
.FormatConditions.Add Type:=xlExpression, Operator:=xlEqual, Formula1:="=OR(ISBLANK($J2),$J2<>'Yes')"
With .FormatConditions(1)
.Interior.Color = RGB(255, 243, 109)
.Font.Bold = True
.StopIfTrue = False
End With
'Not reviewed condition
.FormatConditions.Add Type:=xlExpression, Formula1:="=OR(ISBLANK($L2),$L2<>'Yes'"
With .FormatConditions(2)
.Font.Color = RGB(225, 6, 0)
.Font.Bold = True
.StopIfTrue = False
End With
End With
The code fails on 4th line of code
.FormatConditions.Add Type:=xlExpression, Operator:=xlEqual, Formula1:="=OR(ISBLANK($J2),$J2<>'Yes')"
with error Invalid procedure call or argument.
What am I doing wrong?
Excel won't like the apostrophe delimiters, wants quote marks. Double to escape them as literal text.
Formula1:="=OR(ISBLANK($J2),$J2<>""Yes"")"
I have the following conditions when I create a worksheet via vba, the first two work fine in that if the cell date is two days prior to today's date then it turns orange or if the cell date is due/overdue the cell turns red, what doesn't work at all is the last condition, it is simply ignored. The condition I would like is if the cell P2 is not blank it turns cell O2 green. I have no idea what I am doing wrong, any advice would be greatly appreciated
Set MyRange = Range("o2:o" & J)
Set MyRange1 = Range("P2:P" & J)
MyRange.FormatConditions.Add Type:=xlExpression, Formula1:="=AND($O2>TODAY(),$O2<=
(TODAY()+2))"
MyRange.FormatConditions(1).Interior.Color = RGB(255, 153, 51)
MyRange.FormatConditions.Add Type:=xlCellValue, Operator:=xlLessEqual, _
Formula1:="=NOW()"
MyRange.FormatConditions(2).Interior.Color = RGB(255, 0, 0)
MyRange1.FormatConditions.Add Type:=xlExpression, Formula1:="=P2<>"""
MyRange.FormatConditions(3).Interior.Color = RGB(0, 153, 0)
I have an Excel VBA procedure which is supposed to compare the values of two cells. In my case they are scalars, ranging from 1 to 3. Basically, they are answers to questions. If they match, then I want to color a certain cell green, otherwise I want to make it red. Is there something wrong with my syntax?
Sub CheckBold()
'
' CheckBold Macro
'
'
Row = ActiveCell.Row
If ThisWorkbook.Sheets(1).Range("D" & CStr(ActiveCell.Row)).Font.Bold Then
ActiveCell.Value = 1
End If
If ThisWorkbook.Sheets(1).Range("E" & CStr(ActiveCell.Row)).Font.Bold Then
ActiveCell.Value = 2
End If
If ThisWorkbook.Sheets(1).Range("F" & CStr(ActiveCell.Row)).Font.Bold Then
ActiveCell.Value = 3
End If
ActiveCell.Value = ThisWorkbook.Sheets(3).Range("A" & CStr(ActiveCell.Row)).Value & ActiveCell.Value
If CInt(ActiveCell.Value) = CInt(ThisWorkbook.Sheets(3).Range("A" & CStr(ActiveCell.Row)).Value) Then
ActiveCell.Interior.Color = RGB(0, 180, 0)
Else
ActiveCell.Interior.Color = RGB(180, 0, 0)
End If
End Sub
What happens is that always the code goes on the Then branch of the if, even though the values are different. Why do I get this behavior?
This might be a trivial question for you experts out there:
Based on the input (Week) the table is initially filtered on that specific week(WK) and the next one(WK+1).
I'm then formatting all WK+1 cells to be greyed out.
So far so good. Now the question. How can I change the code below so that the entire row containing a cell with the WK+1 value to be greyed out?
ActiveSheet.Range("B5").AutoFilter Field:=1, Criteria1:=WK, Operator:=xlOr, _
Criteria2:=(WK + 1)
With ActiveSheet.Range("$B:$B").FormatConditions _
.Add(xlCellValue, xlEqual, "=" & WK + 1)
With .Font
.Bold = True
.ColorIndex = 15
End With
End With
Thanks in advance!
Mac
This is possible with FormatCondition of type xlExpression.
Example:
ActiveSheet.Range("$A:$Z").FormatConditions.Delete
ActiveSheet.Range("A1").Activate
With ActiveSheet.Range("$A:$Z").FormatConditions _
.Add(Type:=xlExpression, Formula1:="=($B1=" & WK + 1 & ")")
With .Font
.Bold = True
.ColorIndex = 15
End With
End With
I delete the FormatConditions before I add the new one. Because if not, multiple calls of this code would result in multiple FormatConditions ever with the same condition but possible other values of WK. This is because the code adds a new FormatCondition everytime it runs.
Greetings
Axel
I am using a loop to put a conditional format in "every 4th row" in "column D". However, I am not able to get the code correct.
I had two seperate things happen. First, I have had the formula show up in the spreadsheet with " " around the formula and the conditional format did not work. So now I am trying to rewrite it using the following code and it tells me compile error expected end of statement.
Any help on how I can get the conditional format of D(i-1)>sum(D(i):D(i+2) shade cell red to work is appreciated.
This is the middle of the For/Next loop where I am trying to shade.
Range("D" & (i - 1)).Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="D" & (i-1) & "> Sum(D" & i & "D" & (i + 2)")"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
i = i + 4
This worked for me:
Sub Tester()
Dim i As Long, fc As FormatCondition
For i = 2 To 10 Step 4
Set fc = ActiveSheet.Range("D" & (i - 1)).FormatConditions. _
Add(Type:=xlExpression, _
Formula1:="=D" & (i - 1) & "> Sum(D" & i & ":D" & (i + 2) & ")")
fc.SetFirstPriority
With fc.Interior
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
End With
fc.StopIfTrue = False
Next i
End Sub
Is it a requirement to do this in VBA? I'm asking because, through regular conditional formatting, you could also easily apply it to every 4th row.
To do so, you simply need a formula like this one in your conditional formatting:
=AND(MOD(ROW($D5),4)=0, $D5 > SUM($D$2:$D4))
The MOD-function divides the row number by 4 and checks if the remainder is 4.
It's not exactly what you asked for, but it might solve your situation...