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"")"
Related
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)
Code to apply a conditional format. When range.value is lower than 7,2 it stays white, when higher than 8,1 it turns red.
This code runs on my laptop. On another computer, it throws this error.
Run-time error '5':
Invalid procedure call or argument
Private Sub totalEPS(mySelection As Range)
With mySelection.FormatConditions
.Delete
With .Add(Type:=xlCellValue, Operator:=xlGreaterEqual, Formula1:="=7,2")
.Interior.Color = 65535
.StopIfTrue = False
End With
With .Add(Type:=xlCellValue, Operator:=xlGreater, Formula1:="=8,1")
.Interior.Color = 255
.StopIfTrue = False
End With
End With
End Sub
Debug on the other computer stops in the line
With .Add(Type:=xlCellValue, Operator:=xlGreaterEqual, Formula1:="=7,2")
I suspect it has to do with the decimal separator - that comma would be a dot on an en-US operating system.
Try adding this function to your module:
Public Function LocalizeDecimal(ByVal value As Double) As String
LocalizeDecimal = Replace(Str(value), ".", Application.International(xlDecimalSeparator))
End Function
Then edit the Formula1 argument to something like this:
With .Add(Type:=xlCellValue, Operator:=xlGreaterEqual, Formula1:="=" & LocalizeDecimal(7.2))
And:
With .Add(Type:=xlCellValue, Operator:=xlGreater, Formula1:="=" & LocalizeDecimal(8.1))
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.
UPDATE
Thank you for the assistance. I have updated my code to look like:
Sub AddColor()
With Sheet1.Range("$T$3:$T$3600").FormatConditions
.Delete
With .Add(xlExpression, Formula1:="=AND(($Q3+7)<=TODAY(),$Q3>0,$T3="""")")
.Interior.Color = RGB(0, 176, 240)
.StopIfTrue = False
End With
With .Add(xlExpression, Formula2:="=AND(($Q3+14)<=TODAY(),$Q3>0,$T3="""")")
.Interior.Color = RGB(255, 0, 0)
.StopIfTrue = True
End With
End With
With Sheet1.Range("$U$3:$U$3600").FormatConditions
.Delete
With .Add(xlExpression, Formula1:="=AND(($S3-1)<=TODAY(),$S3>0,$U3="""")")
.Interior.Color = RGB(0, 176, 240)
.StopIfTrue = False
End With
With .Add(xlExpression, Formula2:="=AND(($T3+1)<=TODAY(),$U3="""",$T3>0)")
.Interior.Color = RGB(255, 0, 0)
.StopIfTrue = True
End With
End With
'Code continues
I am now getting an error that says "Argument Not Optional" and it seems to be referring to my "Formula2" line on the first set of statements. I am not sure what argument is missing, as it is working correctly for the first statement. I tried to skip over the second formula and it has the same error for the next set for arguments.
It is probably something simple, but any assistance is appreciated!
UPDATE
I am trying to add conditional formatting through VBA, but am running into some issues with my code. I would like to be able to do it through the conditional formatting function, but the data that is going to be imported requires that i split columns, which causes the reference cells to change, but conditional formatting does not seem to keep it how I want it (long story). Anyway, I have about 10 more of these to format the information in the corresponding columns and am just trying to figure out why I keep getting an error. Here is what I have:
Sub AddColor()
With Sheet1.Range("$T$3:$T$3600")
.FormatConditions.Add xlExpression, Formula1:="=AND(($Q3+7)
<=TODAY(),$Q3>0,$T3="")"
.FormatConditions(1).Interior.Color = RGB(0, 176, 240)
.FormatConditions(1).StopIfTrue = False
.FormatConditions.Add xlExpression, Formula2:="=AND(($Q3+14)
<=TODAY(),$Q3>0,$T3=0)"
.FormatConditions(2).Interior.Color = RGB(255, 0, 0)
.FormatConditions(2).StopIfTrue = True
End With
With Sheet1.Range("$U$3:$U$3600")
.FormatConditions.Add xlExpression, Formula1 = "=AND(($S3-1
<=TODAY(),$S3>0,$U3="")"
.FormatConditions(3).Interior.Color = RGB(0, 176, 240)
.FormatConditions(3).StopIfTrue = False
.FormatConditions.Add xlExpression, Formula2 = "=AND(($T3+1)
<=TODAY(),$U3="",$T3>0)"
.FormatConditions(4).Interior.Color = RGB(255, 0, 0)
.FormatConditions(4).StopIfTrue = True
End With
(the<=TODAY() portion is a continuation i my code, it just jumped to the next line due to formatting.) What am I doing wrong? Any assistance would be greatly appreciated!
Your statement
.FormatConditions.Add xlExpression, Formula1:="=AND(($Q3+7)<=TODAY(),$Q3>0,$T3="")"
is trying to tell Excel to use a formula of =AND(($Q3+7)<=TODAY(),$Q3>0,$T3="). That is syntactically incorrect, as there is no closing quote for the part that starts $T3=".
You need to escape all double-quotation marks (") within string literals in VBA code by using two double-quotation marks (i.e. "") for every one you actually want in the string.
I believe you want your code to be:
Sub AddColor()
With Sheet1.Range("$T$3:$T$3600")
.FormatConditions.Add xlExpression, Formula1:="=AND(($Q3+7)<=TODAY(),$Q3>0,$T3="""")"
.FormatConditions(1).Interior.Color = RGB(0, 176, 240)
.FormatConditions(1).StopIfTrue = False
.FormatConditions.Add xlExpression, Formula2:="=AND(($Q3+14)<=TODAY(),$Q3>0,$T3=0)"
.FormatConditions(2).Interior.Color = RGB(255, 0, 0)
.FormatConditions(2).StopIfTrue = True
End With
With Sheet1.Range("$U$3:$U$3600")
.FormatConditions.Add xlExpression, Formula1:="=AND(($S3-1)<=TODAY(),$S3>0,$U3="""")"
.FormatConditions(3).Interior.Color = RGB(0, 176, 240)
.FormatConditions(3).StopIfTrue = False
.FormatConditions.Add xlExpression, Formula2:="=AND(($T3+1)<=TODAY(),$U3="""",$T3>0)"
.FormatConditions(4).Interior.Color = RGB(255, 0, 0)
.FormatConditions(4).StopIfTrue = True
End With
In addition to the corrections made by YowE3K, I suggest you make a few other improvements. The way you are referring to the newly added CFs is hazardous. I suggest:
1- delete any old CF before adding new ones in the macro. Otherwise they will keep accumulating each time you run the macro and accordingly theit indexes will not be what you "think" they are.
2- Refer explicitly to any newly added CF instead of by index. For example, in the column U, you refer to them as .FormatConditions(3) and (4), which is incorrect.
With Sheet1.Range("$U$3:$U$3600").FormatConditions
.Delete ' <--- delete old CF if any
With .Add(xlExpression, Formula1:="=AND(($S3-1)<=TODAY(),$S3>0,$U3="""")")
.Interior.Color = RGB(0, 176, 240)
.StopIfTrue = True
'...
End With
With .Add(xlExpression, Formula1:="=AND(($T3+1)<=TODAY(),$U3="""",$T3>0)")
.Interior.Color = RGB(255, 0, 0)
.StopIfTrue = True
'...
End With
End with
Apply the same method for each CF you want to add, and don't forget to "double-up" your formula's double-quotes that you embed inside a VBA string.
I have this code for conditionnal formating, where i'm only changing the string:
Cells.FormatConditions.Delete
With Range("$A$1:$H$17").FormatConditions.Add(Type:=xlTextString, String:="CPA", TextOperator:=xlContains)
.Interior.Color = RGB(105, 191, 44)
End With
With Range("$A$1:$H$17").FormatConditions.Add(Type:=xlTextString, String:="CPN", TextOperator:=xlContains)
.Interior.Color = RGB(105, 191, 44)
End With
With Range("$A$1:$H$17").FormatConditions.Add(Type:=xlTextString, String:="CSS", TextOperator:=xlContains)
.Interior.Color = RGB(105, 191, 44)
End With
With Range("$A$1:$H$17").FormatConditions.Add(Type:=xlTextString, String:="RL", TextOperator:=xlContains)
.Interior.Color = RGB(105, 191, 44)
End With
Is there any alternative for all these lines, so i can write it in a shorter and more effective way?
This macro colors even the cells wich contains "CPAzergfzergfer". how can i write a macro where i only color cells containing the exact string ?
You could use an array to specify the conditions for the conditional formatting, like this:
myArray = Array("CPA", "CPN", "CSS", "RL")
For myLoop = LBound(myArray) to UBound(myArray)
With Range("$A$1:$H$17").FormatConditions.Add(Type:=xlTextString, String:=myArray(myLoop), TextOperator:=xlEqual)
.Interior.Color = RGB(105, 191, 44)
End With
Next
I've also changed the TextOperator so it should only select items that match the text value, rather than select items which contain the text value.
Dave's answer is great. Here's another option. This puts all the conditions in one formula, which I think makes it easier to manage in the not-so-great conditional formatting dialog box.
I turned on the macro recorder and opened the CF dialog and entered this formula:
=OR(A1="CPA",A1="CPN",A1="CSS",A1="RL")
Here's the cleaned-up version of thecode that was generated:
Sub Macro1()
ActiveSheet.Range("A1:H17").FormatConditions.Add Type:=xlExpression, Formula1:= _
"=OR(A1=""CPA"",A1=""CPN"",A1=""CSS"",A1=""RL"")"
Selection.FormatConditions(1).Interior.Color = RGB(105, 191, 44)
End Sub