macro formula/conditional formatting

jmkerzic

New Member
Joined
Jul 5, 2019
Messages
31
here is my macro. I have to highlight rows that only have the word "pad" in column M only. I changed it to M:M but it highlighted several columns that did not have the word pad in it.

Selection.FormatConditions(1).StopIfTrue = False
Cells.Select
Selection.FormatConditions.add Type:=xlExpression, Formula1:= _
"=COUNTIF(1:1,""*pad*"")"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 9419919
.TintAndShade = 0
End With
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Try this

Code:
Sub Macro1()
  Dim rng As Range
  Set rng = Cells
  Range("A1").Select
  Cells.FormatConditions.Delete
  rng.FormatConditions.Add Type:=xlExpression, Formula1:="=[COLOR=#333333]COUNTIF[/COLOR]($M1,""*pad*"")"
  rng.FormatConditions(rng.FormatConditions.Count).SetFirstPriority
  With rng.FormatConditions(1).Interior
    .PatternColorIndex = xlAutomatic
    .Color = 9419919
    .TintAndShade = 0
  End With
  rng.FormatConditions(1).StopIfTrue = False
End Sub
 
Upvote 0
I have to highlight rows that only have the word "pad" in column M only.
My interpretation of that is that column M must contain "pad" and no other columns are allowed to contain "pad".

Also. I would recommend against ..
- applying Conditional Formatting to the whole worksheet
- using a COUNTIF function to count a whole row or whole column.

Try to limit the ranges if you can. So if my interpretation above is correct my suggestion would be something like

Code:
Sub CheckPad()
  With Range("A1:BZ1000")
    .FormatConditions.Add Type:=xlExpression, Formula1:="=AND(SEARCH(""pad"",$M1),COUNTIF($A1:$BZ1,""*pad*"")=1)"
    .FormatConditions(.FormatConditions.Count).SetFirstPriority
    With .FormatConditions(1).Interior
      .PatternColorIndex = xlAutomatic
      .Color = 9419919
      .TintAndShade = 0
    End With
  End With
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,785
Messages
6,121,543
Members
449,038
Latest member
Guest1337

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