Macro using InStr not returning expected results.

Jefe187

New Member
Joined
Oct 24, 2015
Messages
13
I have macro that is to reference a list of word on a worksheet ("List")and then check for the list in a another worksheet ("sheet 1"). If a word is present in the list then certain formatting will be done to columns A:E of the sheet 1 worksheet. The macro partially works, except it fills the entire columns A:E in my sheet 1 worksheet. I am not a vba expert, I tried to join all the cells and separate with a "|" then use the InStr function to return a value or a 0. Can someone please provide advice on why this is not working correctly. Here is the code:

Public Sub HighlightListedValues()

Dim strConcatList As String
Dim Cell As Range


'Creates a string concatenating list of strings, separated by |s
'e.g. "item1|item2|item3|item4|"
For Each Cell In Sheets("List").Range("A1:A10")
strConcatList = strConcatList & Cell.Value & "|"
Next Cell


'For each used cell in Column A of sheet1, check whether the value in that cell
'is contained within the concatenated string
For Each Cell In Intersect(Sheets("Sheet1").Range("A:A"), Sheets("Sheet1").UsedRange)
If InStr(strConcatList, Cell.Value) > 0 Then 'InStr returns 0 if the string isn't found
Cell.Columns("A:E").Interior.Color = RGB(0, 0, 128) 'Highlights the row in blue if value found
Cell.Columns("A:E").Font.Color = RGB(255, 255, 255) 'Highlights the font in white if value found
Cell.Columns("A:E").Font.Bold = True 'Bolds the font
End If
Next Cell
End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Try changing these references
Cell.Columns("A:E")

To
Intersect(Cell.EntireRow, Columns("A:E"))
 
Upvote 0
Thank you for the reply. It is still formatting the entire selection, rather than just the row that matches the text in "List" worksheet. I wish I could insert a screen shot, but I have recreated a basic example below. I need the Air Systems and Application Systems formatted as well as the next five columns to the right.

Air Systems 1,775.00 1,756.00
LINES GP-A 1261 25.00 12.00
MANIFOLD G 38111 100.00 200.00
SHIELD GP- 1231 150.00 144.00
TURBO GP 391261 1,500.00 1,400.00
Application Systems 204.00 216.00
BASE GP-V 2961 50.00 50.00
CONNECTION 12311 150.00 150.00
COVER GP 1407461 4.00 16.00

<colgroup><col><col><col><col><col span="2"></colgroup><tbody>
</tbody>
 
Upvote 0

Forum statistics

Threads
1,215,506
Messages
6,125,189
Members
449,213
Latest member
Kirbito

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