VBA code for selecting a row based on text and using conditional formatting

FrenchCelt

Board Regular
Joined
May 22, 2018
Messages
214
Office Version
  1. 365
Platform
  1. Windows
Hello,

I'm trying to add a snippet of VBA code to a macro I've created. I want to select a row (the row # will vary, so I can't use that in the code) that contains a specific string of text from column A ("Total Unapproved Indirect Labor"), and then run conditional formatting to highlight cells greater than 29.99. I cobbled this together, but instead of highlighting the values in the row I want, it's highlighting the values in column B:

Code:
Last = Cells(Rows.Count, "A").End(xlUp).Row    For j = Last To 1 Step -1
        If (Cells(j, "A").Value) = "Total Unapproved Indirect Labor" Then
    'Cells(j, "A").EntireRow.Select
        End If
    Next j
    With Selection
        Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
        Formula1:="=29.99"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    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
    End With

Can anyone pinpoint where I went wrong and suggest a fix?
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
If you uncomment this line
Code:
'Cells(j, "A").EntireRow.Select
It works for me.
 
Upvote 0
Another option would be
Code:
   Dim Fnd As Range
   Set Fnd = Range("A:A").Find("Total Unapproved Indirect Labor", , , xlWhole, , , False, , False)
   If Fnd Is Nothing Then
      MsgBox "not found"
      Exit Sub
   End If
    With Fnd.EntireRow
      .FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
         Formula1:="=29.99"
      .FormatConditions(.FormatConditions.Count).SetFirstPriority
      With .FormatConditions(1).Font
         .Color = -16383844
         .TintAndShade = 0
      End With
      With .FormatConditions(1).Interior
         .PatternColorIndex = xlAutomatic
         .Color = 13551615
         .TintAndShade = 0
      End With
      .FormatConditions(1).StopIfTrue = False
    End With
End Sub
 
Upvote 0
I can't believe I missed the commenting out. :laugh:

Thanks all, uncommenting was all it took to fix the problem.
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top