Trying to create a relatively simple macro to cycle through every sheet apart from the one at the start (named) and apply conditional formatting to all of the used cells within a specific column, based off the numerical average of the cells in question.
I've gotten stuck with getting the conditional formatting formula to enter correctly into the cell.
Example: The formula I want is =R2>2(Average(R2:R*)) (where * is the last used row)
However, what's im getting in the formula box is: ="R2>2(Average(R2:R230))" (230 is the last used row on that sheet)
Removing the speechmarks from the script breaks it with "Expected:End of Statement"
Code here, because there might be other stuff wrong with it too:
Sub Bread()
Dim LastRow As Long
For Each Sheet In Sheets
If Sheet.Name = "Base Details" Then
Else
Sheet.Cells.ClearFormats
LastRow = Sheet.UsedRange.Rows.Count
With Sheet.Range("R2:R" & LastRow)
.FormatConditions.Add Type:=xlExpression, Formula1:="R2" & ">" & "2" & "(" & "Average" & "(" & "R2:R" & LastRow & "))"
With .FormatConditions(.FormatConditions.Count)
.SetFirstPriority
With .Interior
.Color = RGB(255, 0, 0)
End With
End With
End With
With Sheet.Range("R2:R" & LastRow)
.FormatConditions.Add Type:=xlExpression, Formula1:="R2" & "<" & "0.1" & "*" & "(" & "Average" & "(" & "R2:R" & LastRow & "))"
With .FormatConditions(.FormatConditions.Count)
.SetFirstPriority
With .Interior
.Color = RGB(255, 255, 0)
End With
End With
End With
End If
Next Sheet
End Sub
Thanks a lot!
*
between the2
and(
– Scott CranerFormula1:="R2>2*(Average(R2:R" & LastRow & "))"
You say that you are getting 230 and 230 is the last used row on the sheet. You also say that you want the last used row, so I'm confused about what the problem is. – JNevill=
sign there likeFormula1:="=YourFormula"
– JNevill