Apply function/code to every row in a worksheet using VBA - excel

I have a macro which applies conditional formatting to a row which highlights specific values
I was wondering if there was a way to apply it to multiple rows (I want it to run from rows 18-79)
As you can see from my code below, the function has to be adjusted for each row individually (I have done 3).
I was wondering if there was an easier way to apply this rather than repeat and adjust for all the rows I need.
Sub Highlight()
'
' Highlight good values
Application.ScreenUpdating = False
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
ws.Activate
Rows("18:18").Select
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
Formula1:="=$C$18", Formula2:="=$D$18"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = -16752384
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 13561798
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Rows("19:19").Select
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
Formula1:="=$C$19", Formula2:="=$D$19"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = -16752384
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 13561798
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Rows("20:20").Select
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
Formula1:="=$C$20", Formula2:="=$D$20"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = -16752384
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 13561798
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Next ws
Application.ScreenUpdating = True
End Sub

change the range to include it all:
With ActiveSheet.Rows("18:79")
.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
Formula1:="=$C18", Formula2:="=$D18"
.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With .FormatConditions(1).Font
.Color = -16752384
.TintAndShade = 0
End With
With .FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 13561798
.TintAndShade = 0
End With
.FormatConditions(1).StopIfTrue = False
End With

Related

Applying a conditional formatting macro to a table in current sheet instead of a named table

I have recorded a macro that corrects the conditional formatting rules of a table every time they get messed up (because of adding or removing lines I suppose…)
And I put a button to activate the macro in the sheet
I need to replicate the same table in several sheets (increasing number of sheets) and I want my macro to function on all of them (not necessarily simultaneously) in addition of having this common table, most of the sheets have other tables also, but there will be 1 table that will be replicated in MOST sheets.
(Basically create a template sheet containing the table and macro button that users will replicate for each new client
Since the tables will have same number of columns and column titles, is it possible to tweak it so it works on any table where the cursor has selected a cell? Or similar?
Maybe some way of changing the ref from “bookingInfo” to “selected table”
FYI: I do not know how to write VBA at all
Here is the code I have:
Application.ScreenUpdating = False
Application.Goto Reference:="BookingInfo"
Selection.ListObject.Range.FormatConditions.Delete
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=$B4<>$B5"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Bold = True
.Italic = False
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=$A5<>"""""
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.Pattern = xlLightDown
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.14996795556505
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=$AN5=""Full PMT"""
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent3
.TintAndShade = 0.399945066682943
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=$AN5=""Partial PMT"""
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent6
.TintAndShade = 0.399945066682943
End With
Selection.FormatConditions(1).StopIfTrue = False
Application.ScreenUpdating = True
End Sub
Any Help?
You could do it like this:
Sub CFUpdate()
Dim lo As ListObject, rng As Range
Set lo = Selection.ListObject
If lo Is Nothing Then 'is the selection in a listobject?
MsgBox "First select any cell in the Table to be updated", vbExclamation
Exit Sub 'nothing to do...
End If
Set rng = lo.DataBodyRange 'range to be formatted
rng.FormatConditions.Delete
With AddFC(rng, xlExpression, "=$B4<>$B5").Font
.Bold = True
.Italic = False
.TintAndShade = 0
End With
With AddFC(rng, xlExpression, "=$A5<>""""").Interior
.Pattern = xlLightDown
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.14996795556505
End With
With AddFC(rng, xlExpression, "=$AN5=""Full PMT""").Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent3
.TintAndShade = 0.399945066682943
End With
With AddFC(rng, xlExpression, "=$AN5=""Partial PMT""").Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent6
.TintAndShade = 0.399945066682943
End With
Application.ScreenUpdating = True
End Sub
'factoring out some common steps
Function AddFC(rng As Range, fcType As XlFormatConditionType, frmla As String)
Dim fc As FormatCondition
Set fc = rng.FormatConditions.Add(Type:=fcType, Formula1:=frmla)
fc.StopIfTrue = False
Set AddFC = fc 'return the FormatCondition we just added
End Function
Pulled some of the common code out into a separate function to reduce the bulk of the code when adding each format condition.
Note you'll also need to adjust the formulas if the tables don't all start on the same row...

Filling some cells with different colors

I have this code to achieve coloring based on cell value (Input picture). But next I want to achieve what shown in (Output) picture using Macro code in excel. Can someone help me write the code for it?
If LOG STATUS = "PENDING LAST PART FROM MFG" THEN It should fill MFG COLUMN CELLS with same color
If LOG STATUS = "PENDING PACKING" THEN It should fill MFG COLUMN CELLS with same color as log status
If LOG STATUS = "SCHEDULE FOR PU" THEN It should fill SCHEDULE PU DATE COLUMN CELLS with same color as log status for those rows.
Sub TEST()
' Formatting "pending 1st PART"
Columns("N:N").Select
Selection.FormatConditions.Add Type:=xlTextString, String:= _
"PENDING 1ST PART FROM MFG", TextOperator:=xlContains
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
' Formatting "Pending last PART"
Columns("N:N").Select
Selection.FormatConditions.Add Type:=xlTextString, String:= _
"PENDING LAST PART FROM MFG", TextOperator:=xlContains
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
' Formatting "Pending PACKING"
Columns("N:N").Select
Selection.FormatConditions.Add Type:=xlTextString, String:= _
"PENDING PACKING", TextOperator:=xlContains
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 65280
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
' Formatting "PENDING PU REQUEST"
Columns("N:N").Select
Selection.FormatConditions.Add Type:=xlTextString, String:= _
"PENDING PU REQUEST", TextOperator:=xlContains
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
' Formatting "Pending FF"
Columns("N:N").Select
Selection.FormatConditions.Add Type:=xlTextString, String:= _
"PENDING FF", TextOperator:=xlContains
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 39423
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
' Formatting "SCHEDULED FOR PU"
Columns("N:N").Select
Selection.FormatConditions.Add Type:=xlTextString, String:= _
"SCHEDULED FOR PU", TextOperator:=xlContains
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 16776960
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
End Sub
Input
Output

Conditional formatting Run-time error '9' subscript out of range

I'm trying to make a macro in VBA Excel to apply a few conditional formats.
See below:
Private Sub CommandButton1_Click()
'Delete conditional formats
Sheets("Results").Cells.FormatConditions.Delete
'Red formats
Sheets("Results").Select
With ActiveSheet.Range("C:C,A:A")
.FormatConditions.AddUniqueValues
.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
.FormatConditions(1).DupeUnique = xlDuplicate
End With
With Selection.FormatConditions(1).Font
.Color = -16383844
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 13551615
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
'Blue formats
With ActiveSheet.Range("E:E,C:C,A:A")
.FormatConditions.AddUniqueValues
.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
.FormatConditions(1).DupeUnique = xlUnique
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 15773696
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
End Sub
The error pops up in these lines of code:
.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
Sometimes it works sometimes it doesn't.
I recorded this macro and was working fine few days ago.

conditional formatting vba with if

I have a question:
I'm trying to make two different conditional formatting at once.
But it works only the second one.
Im using if and Elseif, and working only endif
What i have to write to get work them both? First one after that another?
For Each cell In Range("A1:AA1")
If cell.Value = "GM WP6 Sensor Status" Then
Cells.FormatConditions.Delete
Columns("H:H").Select
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlNotEqual, _
Formula1:="=32671"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
End With
'Selection.FormatConditions(1).StopIfTrue = False
Columns("H:H").Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=LEN(TRIM(H1))=0"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.Pattern = xlNone
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Range("A1").Select
''''''''''''''''''''''''''Tikrina kita
ElseIf cell.Value = "GM WP6 Sensor Status light" Then
Cells.FormatConditions.Delete
Columns("I:I").Select
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlNotEqual, _
Formula1:="=32767"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Columns("I:I").Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=LEN(TRIM(I1))=0"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.Pattern = xlNone
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Range("A1").Select
End If
Next cell
Please give this a try...
Sub ConditionalFormatting()
Columns("H:H").FormatConditions.Delete
Columns("I:I").FormatConditions.Delete
If Application.CountIf(Range("A1:AA1"), "GM WP6 Sensor Status") > 0 Then
Columns("H:H").FormatConditions.Add Type:=xlExpression, Formula1:= _
"=AND(LEN(TRIM(H1))>0,H1<>32671)"
Columns("H:H").FormatConditions(Columns("H:H").FormatConditions.Count).SetFirstPriority
With Columns("H:H").FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
End With
End If
If Application.CountIf(Range("A1:AA1"), "GM WP6 Sensor Status light") > 0 Then
Columns("I:I").FormatConditions.Add Type:=xlExpression, Formula1:= _
"=AND(LEN(TRIM(I1))>0,I1<>32671)"
Columns("I:I").FormatConditions(Columns("I:I").FormatConditions.Count).SetFirstPriority
With Columns("I:I").FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
End With
End If
End Sub

Excel 2010 VBA Macro Conditional Formatting of row if cell contains text snippet

I'm doing conditional formatting in a macro (because I'm constantly applying it along with other formatting repeatedly to a fresh, raw export).
Objective: highlight any row where the text in cell J(n) is "No Activity"
Currently using:
With Cells
.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=($J1=""No Activity"")"
With .FormatConditions(.FormatConditions.Count)
.SetFirstPriority
With .Interior
.PatternColorIndex = xlAutomatic
.Color = 7405514
.TintAndShade = 0
End With
StopIfTrue = False
End With
End With
...which works great. The above was cleaned up using a google search and a recording that originally gave me:
Cells.Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=($N1=""No Activity"")"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent4
.TintAndShade = 0.599963377788629
End With
Selection.FormatConditions(1).StopIfTrue = False
So I was feeling all proud and accomplished... but I also want to highlight rows (in a different color) where the cell in Column J (per above) contains "Quote" at any point in the text of the cell.
When I recorded a macro of doing it as conditional formatting, it didn't really clarify anything for me: (ok, it made it worse)
Selection.FormatConditions.Add Type:=xlTextString, String:="Quote", _
TextOperator:=xlContains
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent1
.TintAndShade = 0.399945066682943
End With
Selection.FormatConditions(1).StopIfTrue = False
I'm just not catching how it should change in
Type:=xlExpression, Formula1:= _
"=($J1=""No Activity"")"
All ideas greatly appreciated!
This works in Excel 2010:
With Cells
.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=($J1=""No Activity"")"
With .FormatConditions(.FormatConditions.Count)
.SetFirstPriority
With .Interior
.PatternColorIndex = xlAutomatic
.Color = 7405514
.TintAndShade = 0
End With
StopIfTrue = False
End With
.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=ISNUMBER(SEARCH(""*quote*"",$J1))"
With .FormatConditions(.FormatConditions.Count)
.SetFirstPriority
With .Interior
.PatternColorIndex = xlAutomatic
.Color = 4405514
.TintAndShade = 0
End With
StopIfTrue = False
End With
End With
Obviously you'd need to change the Color for the 2nd FormatConditions.Add section.
Edit: Realized you were looking for "Quote" anywhere in the cell, so I've updated the code from my original posting.

Resources