VBA needs to consider only exact math

Dannottheman

Board Regular
Joined
Dec 24, 2020
Messages
55
Office Version
  1. 2007
Hello,

I am currently using the VBA below which has been working great until now. The script is supposed to find and shading rows containing certain words but now it's shading incorrect rows. For example, it's supposed to shade a cell containing the word "test", but if the cell has the word "testicle", it should NOT shade the cell. Right now it's shading cells because some characters match the word (e.g. unit vs united). Can someone please modify the VBA below so that it only considers the whole word/exact match?


Option Explicit
Sub Dannottheman3()
Dim Cl As Range
Dim srch As Variant
For Each Cl In Range("A1", Range("A" & Rows.Count).End(xlUp))
For Each srch In Array("*quiz*", "*unit*", "*exam*", "*assessment*", "*test*") ' <= note the different use of the jolly (could be useful)
If LCase(Cl.Value) Like srch Then
Cl.Interior.Color = rgbLightBlue
Cl.Font.Bold = True
Exit For
End If
Next srch
Next Cl
End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Untested but I am pretty sure this will work. First, remove the asterisks from your Array function call...

For Each srch In Array("quiz", "unit", "exam", "assessment", "test")

Then replace your If..Then statement with this one...

If " " & LCase(Cl.Value) & " " Like "*[!a-z0-9]" & srch & "[!a-z0-9]*" Then

Those two changes should make your code work the way you indicated you want.
 
Upvote 0
Solution
Untested but I am pretty sure this will work. First, remove the asterisks from your Array function call...

For Each srch In Array("quiz", "unit", "exam", "assessment", "test")

Then replace your If..Then statement with this one...

If " " & LCase(Cl.Value) & " " Like "*[!a-z0-9]" & srch & "[!a-z0-9]*" Then

Those two changes should make your code work the way you indicated you want.
Yes, its working great. Thank you so much!
 
Upvote 0
@Dannottheman
For future reference, you need to mark the post that helped you the most as the solution, not your own post saying it worked. I have changed it for you this time.
 
Upvote 0

Forum statistics

Threads
1,213,522
Messages
6,114,112
Members
448,549
Latest member
brianhfield

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