Color the cell based on text contain..

VBABEGINER

Well-known Member
Joined
Jun 15, 2011
Messages
1,284
Office Version
  1. 365
Platform
  1. Windows
Dear All,

I need help to highlight cells which contain specific text and make them color.

Example-
Col A
abc or p
pqr in op
in abc
pqr or

I want to make color only those cells which will contains " in " & " or ".

Not "in anytext" OR "anytext in" OR "or anytext" OR "anytext or".

small letter only.

Can we make any code for this please..

I have write this code..but this is not worked..
Range("A2").Select
i = 2 'start row number
For Each c In ActiveSheet.Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
Rng = "A" & i
If Range("A" & i).Value = " in " Then
Range(Rng).Interior.ColorIndex = 1
Else
End If
i = i + 1
Next c
 
Thanks Scott for your reply.

I define this list in Name Manager. SO basically it is Named Range..So only those gets highlight apart to that should not highlight..

You would have to loop through the values. Is mylist a named range, an array or what?
 
Upvote 0

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Code:
Sub ColorInOr()
Dim c as Range
Application.ReplaceFormat.Clear
Application.ReplaceFormat.Interior.Color = vbYellow
For Each c In Range("mylist")
    Columns("A").Replace "* " & c & " *", "", SearchFormat:=False, ReplaceFormat:=True
Next
Application.ReplaceFormat.Clear
End Sub
 
Last edited:
Upvote 0
Thank You so much dear...
This works for me...

one more additional, if it is possible..Can I minimize this code..
Dim MLst1 As Range
Application.ReplaceFormat.Clear
Application.ReplaceFormat.Interior.Color = vbGreen
For Each MLst1 In Range("Mylist")
Columns("A").Replace "* " & MLst1 & " *", "", SearchFormat:=False, ReplaceFormat:=True
Next
Application.ReplaceFormat.Clear


Dim MLst2 As Range
Application.ReplaceFormat.Clear
Application.ReplaceFormat.Interior.Color = vbGreen
For Each MLst2 In Range("Mylist")
Columns("A").Replace "* " & MLst2, "", SearchFormat:=False, ReplaceFormat:=True
Next
Application.ReplaceFormat.Clear

Where MLst = your c variable..


Code:
Sub ColorInOr()
Dim c as Range
Application.ReplaceFormat.Clear
Application.ReplaceFormat.Interior.Color = vbYellow
For Each c In Range("mylist")
    Columns("A").Replace "* " & c & " *", "", SearchFormat:=False, ReplaceFormat:=True
Next
Application.ReplaceFormat.Clear
End Sub
 
Upvote 0
This would seem to be inconsistent with your original post.
I want to make color only those cells which will contains " in " & " or ".

Not "in anytext" OR "anytext in" OR "or anytext" OR "anytext or".

That would seem to indicate that you wouldn't want to color cells that end in your word.
 
Upvote 0

Forum statistics

Threads
1,224,834
Messages
6,181,243
Members
453,026
Latest member
cknader

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