Conditional formatting VBA

Lenna

Active Member
Joined
Jun 25, 2014
Messages
269
I've recorded a macro that highlights cells in row Q if they contain a word "donor" or "Transplant" and the code is too long. Can the same be accomplished with less code?

Please propose something better.

Thanks,

Lenna

Columns("Q:Q").Select ‘highlights all cell that contain “donor”
Selection.FormatConditions.Add Type:=xlTextString, String:="Donor", _
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 ‘highlight al cell that contain “transplant”
Selection.FormatConditions.Add Type:=xlTextString, String:="Transplant", _
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
Range("Q20").Select
Cells.FormatConditions.Delete
Columns("Q:Q").Select
Range("Q20").Activate
Selection.FormatConditions.Add Type:=xlTextString, String:="Donor", _
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
Columns("Q:Q").Select
'find donor

Columns("Q:Q").Select
Selection.Find(What:="Donor", After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Offset(1, 0).Select

Selection.FormatConditions.Add Type:=xlTextString, String:="Transplant", _
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
Columns("Q:Q").Select
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hi there,

Try this

Code:
Sub Find_And_Highlight()

Dim r As Range
Dim Lastrow As Long

Lastrow = Cells(Rows.Count, "Q").End(xlUp).Row

For Each r In Range("Q1:Q" & Lastrow)

    If r.Value = "Donor" Then r.Interior.Color = 65535
    If r.Value = "Transplant" Then r.Interior.Color = 65535

Next r

End Sub

b

I DIDN'T TEST IT SO TRY IT IN A COPY OF YOUR WORKBOOK PLEASE.

Regards,
 
Upvote 0

Forum statistics

Threads
1,214,626
Messages
6,120,602
Members
448,974
Latest member
ChristineC

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