non-case sensitive search using VBA

ferrigeu

Board Regular
Joined
Jun 14, 2017
Messages
50
Office Version
  1. 2016
Platform
  1. Windows
i have having sort of an "off day"... i just cant seem to figure out how to add code to make the search non-case sensitive.
it all is good if i by chance type the search criteria in the correct case. if not, it will not be found....
below is my code
ideas pls?

Sub HighlightSearchResult()
Dim strTest As String
Dim strLen As Integer
strTest = Range("M1")
strLen = Len(strTest)
For Each cell In Range("E1:H100")
If InStr(cell, strTest) > 0 Then
cell.Characters(InStr(cell, strTest), strLen).Font.Color = vbGreen
End If
Next
End Sub
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
How about
VBA Code:
Sub HighlightSearchResult()
Dim strTest As String
Dim strLen As Integer, x As Long
strTest = Range("M1")
strLen = Len(strTest)
For Each cell In Range("E1:H100")
   x = InStr(1, cell, strTest, vbTextCompare)
   If x > 0 Then cell.Characters(x, strLen).Font.Color = vbGreen
End If
Next
End Sub
 
Upvote 0
compile error: "End If without block If" - never seen that before...
 
Upvote 0
Oops, delete the "End If" line, I forgot to take it out.
 
Upvote 0
You can use the "Option Text Compare" to avoid case sensitivity

 
Upvote 0
i used the "FLUFF" method...
also - how can i run the vba code automatically as soon as i change the content of cell $M$1 (i.e. as soon as i hit enter?)
 
Upvote 0
How about
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   Dim strLen As Long, x As Long
   Dim Cl As Range
   
   If Target.CountLarge > 1 Then Exit Sub
   If Target.Address(0, 0) = "M1" Then
      strLen = Len(Target.Value)
      For Each Cl In Range("E1:H100")
         x = InStr(1, Cl, Target.Value, vbTextCompare)
         If x > 0 Then Cl.Characters(x, strLen).Font.Color = vbGreen
      Next
   End If
End Sub
This needs to go in the relevant sheet module
 
Upvote 0

Forum statistics

Threads
1,215,455
Messages
6,124,937
Members
449,195
Latest member
Stevenciu

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