InStr gives variable results

hstaubyn

Board Regular
Joined
Sep 13, 2010
Messages
93
Hi,

I have a slight problem using InStr in VBA, code below:

Code:
Dim Text1, Text2, Text3 as String
 
For Each iRange In Range("Org_List")
    If InStr(1, iRange.Value, Text1) <> 0 And Text1 <> "" Then
        Call Insert_Checkbox(iRange.Row)
    Else
        If InStr(1, iRange.Value, Text2) <> 0 And Text2 <> "" Then
            Call Insert_Checkbox(iRange.Row)
        Else
            If InStr(1, iRange.Value, Text3) <> 0 And Text3 <> "" Then
                Call Insert_Checkbox(iRange.Row)
            End If
        End If
    End If
Next iRange

The 3 different text values are taken from cells on the worksheet.

The problem is that if the text value appears at the start of an iRange, sometimes InStr gives '0' as its position in the string. I do not know why this would be, surely it should be '1'?? Also, sometimes if the text value is a single letter say, which appears more than once, InStr can give different results, seemingly depending on what mood it's in...

This is causing issues because even when the text does appear in iRange, it returns a '0' and then doesn't call the Insert_Checkbox sub. Is this a problem with InStr or have I missed something?

Thanks,
H
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
It may be related to the way you are declaring your variables. To Dim them all as strings

Code:
Dim Text1 As String, Text2 As String, Text3 As String
 
Upvote 0
Thanks for the reply,

I changed it and that didn't do anything...

I think it might be something to do with InStr not knowing which position of text1 etc. in the subject string to shoose from if there are several, but that would not explain why it returns '0' when the text is at the start of the string.
 
Upvote 0
Hi

If InStr returns 0 it's because the text was not found.

Maybe a problem with spaces, invisible characters, case sensitivity?

Please post examples.
 
Upvote 0
Ahh, is InStr case sensitive? Didn't realise... I will try including UCase into the code.

it works now, thanks for the help.

Code:
Dim Text1, Text2, Text3 as String
 
For Each iRange In Range("Org_List")
    If InStr(1, UCase(iRange.Value), UCase(Text1)) <> 0 And Text1 <> "" Then
        Call Insert_Checkbox(iRange.Row)
    Else
        If InStr(1, UCase(iRange.Value), UCase(Text2)) <> 0 And Text2 <> "" Then
            Call Insert_Checkbox(iRange.Row)
        Else
            If InStr(1, UCase(iRange.Value), UCase(Text3)) <> 0 And Text3 <> "" Then
                Call Insert_Checkbox(iRange.Row)
            End If
        End If
    End If
Next iRange
 
Upvote 0
UCase is a good way to go. However, if you are not dealing with unusual (ie unusual to us standard English users) text (ref) then you could use the 4th (optional) argument for InStr.

For example
Code:
Sub test()
    Dim LookInVal As String, LookForVal As String
    
    LookInVal = "AbC"
    LookForVal = "B"
    
    MsgBox InStr(1, LookInVal, LookForVal)
    MsgBox InStr(1, LookInVal, LookForVal, vbTextCompare)
End Sub
 
Last edited:
Upvote 0
Hi

I'm glad it's working.

Another way, if in that particular module you are sure that you want all text comparisons not to be case sensitive, you just have to add at the beginning of the module:

Code:
Option Compare Text
 
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,882
Members
452,948
Latest member
Dupuhini

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