InStr Function to search not case sensitive

sermest

New Member
Joined
May 23, 2005
Messages
4
Hello,

I am using InStr function, but it is searching case sensititive. I want to search not case sensitive. My function is like below. Can you help?

Function ContainsText(Rng As Range, Text As String) As String
Dim T As String
Dim myCell As Range
For Each myCell In Rng
If InStr(myCell.Text, Text) > 0 Then
If Len(T) = 0 Then
T = myCell.Address(False, False)
Else
T = T & "," & myCell.Address(False, False)
End If
End If
Next myCell
ContainsText = T
End Function

Thanks in advance.
sermest
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Why are you using code to do this?

You could use worksheet functions.
 
Upvote 0
Whether you do this in VBA, or in Excel functions (as Norie suggested), if you do not care about case, you can get around this by converting both pieces to Upper case before comparing.

In Excel, that would be the UPPER function.
In VBA, it is UCASE.

To use it in VBA int he context of your function would look something like:
If InStr(Ucase(myCell.Text), Ucase(Text)) > 0 Then
 
Upvote 0
This is a very old post, but if others read it, you can also do this with the built in functionality of Instr().
The Instr function defaults the compare parameter to "vbbinarycompare" which is case sensitive, but you can change it to "vbtextcompare", which is not case sensative.

It would look like
If InStr(1,myCell.Text, Text,vbtextcompare) > 0 Then

and this would return true if "Text" is contained within myCell.text.
-Joe
 
Upvote 0
Just further to JoeCross's comment above (and to set the record straight) the Instr function defaults to the system-wide default setting (which may be vbTextCompare or may be vbBinaryCompare). (refer to the old "set compare to" command). It is best to either change the default to vbTextCompare or to implicitly include that parameter in your InStr() call (ie, if you are not concerned with the text foiund matches exactly in it "case" - Ucase, or lcase)
 
Upvote 0

Forum statistics

Threads
1,226,564
Messages
6,191,773
Members
453,679
Latest member
aalligood

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