VBA code to highlight a specific text in cell

vane0326

Well-known Member
Joined
Aug 29, 2004
Messages
819
Is there a VBA code if in collumn (A) has the word "Dog" then say it found it in (A16) then hilghlight the rows thru (A16 to G16). Conditional formatting wont work for what i'm asking for but I wont bore you for my reasons why, But I'll keep this short. Is there a VBA code for that?

Thanks!
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
I don't know where else to go with this, I don't understand the follow-up question because the values in H1 and H2 are how the criteria are determined by the user, which means the macro is flexible to do what you say you want. Change column H as the criteria-containing column to some other convenient column if you need, and modify the code accordingly.
 
Upvote 0
I'm sorry guys I guess I'm not explaing correctly. When you go to in excel click the whole collumn in (A) then hit Edit then hit go to: you have those options to pick. say: Texts, numbers, formulas, or error and when you choose numbers it will only highlight the numbers in collumn (A) then from there you could do anything choosing what type of borders or changing the font size colors or just click clear cells. Now if we can use that go to special comand function using vba to search for a specfic text and when it highlight that text it highlight that row to the criteria I'm asking for. then I will record a simple macro then I start formating like changing the font size and put whatever border I pick, then I will take that macro that I just recorded and add it to the code. So next time that word pops up in collumn (A) I will run that macro and will it format that text including the row.

Hopefully you understand what I'm saying. :biggrin:

Any Ideas?
 
Upvote 0
Does this work??

Code:
Sub Macro2()
Dim iCELL As Range, DataRange As Range

Set DataRange = Intersect(Columns("A:A"), ActiveSheet.UsedRange)
For Each iCELL In DataRange
If InStr(1, LCase(iCELL), "dog") > 0 Or InStr(1, LCase(iCELL), "cat") > 0 Then
Range(iCELL, iCELL.Offset(0, 6)).Select
'Insert the code to format
Else
'Range(iCELL, iCELL.Offset(0, 6)).Interior.ColorIndex = xlNone
End If
Next iCELL
End Sub
 
Upvote 0
I'm keep on trying and it only still highlight cat also I replace "cat" in the code and change it to "Without Pattern Changes" and does not highlight. But it highlight "dog". Why is that ?
Sub Macro2()
Dim iCELL As Range, DataRange As Range

Set DataRange = Intersect(Columns("A:A"), ActiveSheet.UsedRange)
For Each iCELL In DataRange
If InStr(1, LCase(iCELL), "dog") > 0 Or InStr(1, LCase(iCELL), "Without Pattern Changes") > 0 Then
Range(iCELL, iCELL.Offset(0, 6)).Select
'Insert the code to format
Else
'Range(iCELL, iCELL.Offset(0, 6)).Interior.ColorIndex = xlNone
End If
Next iCELL
End Sub


Any Ideas?
 
Upvote 0
I don't know why chiello's code would highlight just one. I thought his code was pretty slick, and I can't think of why it would only highlight "cat" cells.

What happens if you comment out the Or part of his statement, like this:

Code:
If InStr(1, LCase(iCELL), "dog") > 0  'Or InStr(1, LCase(iCELL), "cat") > 0 Then

Also, (And I have no idea why this would make a difference)

but what if instead of using the Or Statement, you used two If statements.

If Cat then ....

If Dog then ...
 
Upvote 0
Sorry, I don't understand why it doesn't work, It's over my knowledge!!

Can you please provide the whole text of a cell containing "dog" which is not highlighted??
 
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,877
Members
449,056
Latest member
ruhulaminappu

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