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.
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 am currently trying to use the font color as a condition to check against in an if statement. It seems incredibly easy, but VBA doesn't seem to be able to do it.
I can't show my actual code as it has proprietary information in it, but I have even tried a simple version of my code with no avail. That code is shown below.
Sub Testing()
Cells(1,1).Font.Color = -16776961
If Cells(1,1).Font.Color = -16776961 Then
Cells(1,3) = "Worked!"
Else
Cells(1,3) = "Didn't Work!"
End If
End Sub
The first line of code actually changes the font color or cell A1 to red. However the conditional statement doesn't work for some reason.
Use the RGB function instead. (Not sure where the negative value comes from anyway?)
Sub Testing()
Cells(1, 1).Font.Color = RGB(255, 0, 0)
If Cells(1, 1).Font.Color = RGB(255, 0, 0) Then
Cells(1, 3) = "Worked!"
Else
Cells(1, 3) = "Didn't Work!"
End If
End Sub
You should use RGB like this:
Cells(1,1).Font.Color = RGB(255, 0, 0)
If Cells(1,1).Font.Color = RGB(255, 0, 0) Then
Cells(1,3) = "Worked!"
Else
Cells(1,3) = "Didn't Work!"
End If
I have written a simple Excel VBA macro to have different formatting in a single cell. I have a formula that returns one of 4 possible values:
l1
l2
l3
l4
I wish to format this as Wingdings for the first character with a colour based on the number - 1 = red, 2 = orange, 3 = yellow and 4 = green.
What I have done is to put in an event trigger on cell change:
Private Sub Worksheet_Change(ByVal Target As Range)
Call Wingdings(Target)
End Sub
The subroutine is as follows:
Sub Wingdings(rCll As Range)
If rCll.Value = "l1" Or rCll.Value = "l2" Or _
rCll.Value = "l3" Or rCll.Value = "l4" Then
Select Case Right(rCll.Value, 1)
Case 1 'Red
With rCll.Characters(Start:=1, Length:=1).Font
.Name = "Wingdings"
.Color = RGB(255, 0, 0)
End With
Case 2 'Orange
With rCll.Characters(Start:=1, Length:=1).Font
.Name = "Wingdings"
.Color = RGB(255, 153, 0)
End With
Case 3 'Yellow
With rCll.Characters(Start:=1, Length:=1).Font
.Name = "Wingdings"
.Color = RGB(255, 255, 0)
End With
Case 4 'Green
With rCll.Characters(Start:=1, Length:=1).Font
.Name = "Wingdings"
.Color = RGB(0, 255, 0)
End With
End Select
End If
End Sub
My issue here is that if I manually enter eg l4, the code runs perfectly. But as soon as I determine this value using a formula, it stops working as I desire. I end up with this:
Can anyone suggest a solution to allow me to format the result of the formula as I desire?
What you have works.
Maybe call something like this on the Change event instead, conditionally, if you are dragging formulas
Sub fixstuff()
For Each cell In Selection
cell.Select
Call Wingdings(Range(cell.Address))
Next cell
End Sub
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