going crazy with Instr 
Author Message
 going crazy with Instr

Hi, JMB,

What you're missing is the double quotes around "ab". Then VB assumes that
ab is a Variant variable, to which it assigns the value Empty. This kind of
error is why I always recommend using Option Explicit, preferably by
checking the "Require variable declaration" option in Tools/Options.

Also, InStr() returns a Variant (Long) value representing the position of
the substring in the larger string, not a True or False value. Since False
evaluates to zero, it doesn't cause a type mismatch in your first example
(although it might be useful if it did).

Actually, when I run your examples, I don't get a message box in the first
one. Oddly, InStr() returns the value 1 when ab is Empty. Apparently it
thinks that Empty is a substring of every string -- which, in a mathematical
sense, it is. Then the If statement tests this value 1 against False, which
is 0, so it skips the Then clause.

Regards,
Jay


Quote:
> I've used Instr many times in macros to check if a certain string was
> in the selected text.

> I fear I'm losing my mind, since the simplest ones no longer work (I
> must, in my old age, be forgetting something quite basic)

> Let's say I select a string "abcd" in a document

> If I code

>       If Instr(selection.range, ab) = False Then
>       Msgbox "False"
>       End if

> if I run that macro it shouldn't have the messagebox, but it does.

> Likewise if if I code

> If Instr(selection.range, ab) Then
> Msgbox "True"
> End If

> if will give me the message box even if the text I've selected doesn't
> have ab in it.

> What am I missing?

> Thanks.



Sun, 02 Nov 2003 11:33:33 GMT  
 going crazy with Instr
I've used Instr many times in macros to check if a certain string was
in the selected text.

I fear I'm losing my mind, since the simplest ones no longer work (I
must, in my old age, be forgetting something quite basic)

Let's say I select a string "abcd" in a document

If I code

      If Instr(selection.range, ab) = False Then
      Msgbox "False"
      End if

if I run that macro it shouldn't have the messagebox, but it does.

Likewise if if I code

        If Instr(selection.range, ab) Then
        Msgbox "True"
        End If

if will give me the message box even if the text I've selected doesn't
have ab in it.

What am I missing?

Thanks.



Sun, 02 Nov 2003 11:10:17 GMT  
 going crazy with Instr
the actual form of the instr command is:

instr(start,string1, string2,[optional for type of comparison])

where start  is the position in string1 from which the comparison is to be
made.

instr actually returns a Variant (Long) specifying the position of the first
occurrence of one string within another


Quote:
> I've used Instr many times in macros to check if a certain string was
> in the selected text.

> I fear I'm losing my mind, since the simplest ones no longer work (I
> must, in my old age, be forgetting something quite basic)

> Let's say I select a string "abcd" in a document

> If I code

>       If Instr(selection.range, ab) = False Then
>       Msgbox "False"
>       End if

> if I run that macro it shouldn't have the messagebox, but it does.

> Likewise if if I code

> If Instr(selection.range, ab) Then
> Msgbox "True"
> End If

> if will give me the message box even if the text I've selected doesn't
> have ab in it.

> What am I missing?

> Thanks.



Sun, 02 Nov 2003 11:38:14 GMT  
 going crazy with Instr
Thanks for the responses.

I guess when I used it before I was using variables, not literal
strings, and I lucked out with the true/false thing.

On Tue, 15 May 2001 23:33:33 -0400, "Jay Freedman"

Quote:

>Hi, JMB,

>What you're missing is the double quotes around "ab". Then VB assumes that
>ab is a Variant variable, to which it assigns the value Empty. This kind of
>error is why I always recommend using Option Explicit, preferably by
>checking the "Require variable declaration" option in Tools/Options.

>Also, InStr() returns a Variant (Long) value representing the position of
>the substring in the larger string, not a True or False value. Since False
>evaluates to zero, it doesn't cause a type mismatch in your first example
>(although it might be useful if it did).

>Actually, when I run your examples, I don't get a message box in the first
>one. Oddly, InStr() returns the value 1 when ab is Empty. Apparently it
>thinks that Empty is a substring of every string -- which, in a mathematical
>sense, it is. Then the If statement tests this value 1 against False, which
>is 0, so it skips the Then clause.

>Regards,
>Jay



>> I've used Instr many times in macros to check if a certain string was
>> in the selected text.

>> I fear I'm losing my mind, since the simplest ones no longer work (I
>> must, in my old age, be forgetting something quite basic)

>> Let's say I select a string "abcd" in a document

>> If I code

>>       If Instr(selection.range, ab) = False Then
>>       Msgbox "False"
>>       End if

>> if I run that macro it shouldn't have the messagebox, but it does.

>> Likewise if if I code

>> If Instr(selection.range, ab) Then
>> Msgbox "True"
>> End If

>> if will give me the message box even if the text I've selected doesn't
>> have ab in it.

>> What am I missing?

>> Thanks.



Sun, 02 Nov 2003 14:40:27 GMT  
 going crazy with Instr
I was using variables before, so I hadn't known to use quotes for a
literal string.

When the quotes are used, then it works like I remembered:

                If Instr(x,y)

If you want to return a true value (do something based on the string
being there), don't put "= True Then" at the end, just a plain "Then"
and it will do whatever you code after that.

However, if you want to trigger action if the string is not found,
then  "= False Then"



Quote:
>Thanks for the responses.

>I guess when I used it before I was using variables, not literal
>strings, and I lucked out with the true/false thing.

>On Tue, 15 May 2001 23:33:33 -0400, "Jay Freedman"

>>Hi, JMB,

>>What you're missing is the double quotes around "ab". Then VB assumes that
>>ab is a Variant variable, to which it assigns the value Empty. This kind of
>>error is why I always recommend using Option Explicit, preferably by
>>checking the "Require variable declaration" option in Tools/Options.

>>Also, InStr() returns a Variant (Long) value representing the position of
>>the substring in the larger string, not a True or False value. Since False
>>evaluates to zero, it doesn't cause a type mismatch in your first example
>>(although it might be useful if it did).

>>Actually, when I run your examples, I don't get a message box in the first
>>one. Oddly, InStr() returns the value 1 when ab is Empty. Apparently it
>>thinks that Empty is a substring of every string -- which, in a mathematical
>>sense, it is. Then the If statement tests this value 1 against False, which
>>is 0, so it skips the Then clause.

>>Regards,
>>Jay



>>> I've used Instr many times in macros to check if a certain string was
>>> in the selected text.

>>> I fear I'm losing my mind, since the simplest ones no longer work (I
>>> must, in my old age, be forgetting something quite basic)

>>> Let's say I select a string "abcd" in a document

>>> If I code

>>>       If Instr(selection.range, ab) = False Then
>>>       Msgbox "False"
>>>       End if

>>> if I run that macro it shouldn't have the messagebox, but it does.

>>> Likewise if if I code

>>> If Instr(selection.range, ab) Then
>>> Msgbox "True"
>>> End If

>>> if will give me the message box even if the text I've selected doesn't
>>> have ab in it.

>>> What am I missing?

>>> Thanks.



Sun, 02 Nov 2003 15:51:07 GMT  
 going crazy with Instr
Hi JMB,

In addition to Jay's answer, remember that InStr returns a numeric value,
being zero if the second string is not in the first, and equal to the
character position of the first character of the first occurrence of the
second string in the first otherwise. Thus

InStr("abcd", "de") = 0
InStr("abcd", "cd") = 3
InStr("abcd", "abc") = 1
InStr("abcd", "a") = 1
InStr("abcdabcd", "de") = 0
InStr("abcdabcd", "da") = 4
InStr("abcdabcd", "cd") = 3
InStr("abcdabcd", "a") = 1

When you compare the output with False, you are forcing a type conversion,
which takes extra time. For practical effect comparing with False is the
same as comparing with zero, so you might as well do that and avoid the type
conversion.

--
Regards
Jonathan West - Word MVP
MultiLinker - Automated generation of hyperlinks in Word
Conversion to PDF & HTML
http://www.multilinker.com
Word FAQs at http://www.multilinker.com/wordfaq
Please post any follow-up in the newsgroup. I do not reply to Word questions
by email


Quote:
> I've used Instr many times in macros to check if a certain string was
> in the selected text.

> I fear I'm losing my mind, since the simplest ones no longer work (I
> must, in my old age, be forgetting something quite basic)

> Let's say I select a string "abcd" in a document

> If I code

>       If Instr(selection.range, ab) = False Then
>       Msgbox "False"
>       End if

> if I run that macro it shouldn't have the messagebox, but it does.

> Likewise if if I code

> If Instr(selection.range, ab) Then
> Msgbox "True"
> End If

> if will give me the message box even if the text I've selected doesn't
> have ab in it.

> What am I missing?

> Thanks.



Sun, 02 Nov 2003 17:53:52 GMT  
 going crazy with Instr
Hi Jonathan,

I'm always happy when I see your name on a reply because I know there
will be solid useful information inside!

Your examples below have made clear to me things about Instr that I've
been confused about forever, even after looking at several help books.

Thanks!

From what you've said at the end of your post, I can change my False's
to 0's and the macros will run faster.

What I'm still not sure of is, sometimes I didn't put any "=" phrase
after the "If Instr".  For example, if I have the code:

 If Instr("string 1", "string 2") Then    
 'do x'

what happens is, if string 2 is present, then it runs the 'do x' code.
If string 2 is not present, 'do x' is skipped.

So is it that the code knows that any value other than "0" should be
considered to satisfy the "If"?

Jerry



Quote:
>Hi JMB,

>In addition to Jay's answer, remember that InStr returns a numeric value,
>being zero if the second string is not in the first, and equal to the
>character position of the first character of the first occurrence of the
>second string in the first otherwise. Thus

>InStr("abcd", "de") = 0
>InStr("abcd", "cd") = 3
>InStr("abcd", "abc") = 1
>InStr("abcd", "a") = 1
>InStr("abcdabcd", "de") = 0
>InStr("abcdabcd", "da") = 4
>InStr("abcdabcd", "cd") = 3
>InStr("abcdabcd", "a") = 1

>When you compare the output with False, you are forcing a type conversion,
>which takes extra time. For practical effect comparing with False is the
>same as comparing with zero, so you might as well do that and avoid the type
>conversion.



Mon, 03 Nov 2003 11:46:34 GMT  
 going crazy with Instr

Jerry,

In that case InStr is essentially being converted to a Boolean, just
like it is when you use "= False", and places -- I would assume --
the same kind of extra load on the processor.  The reason it looks
like a slightly different case is because Booleans can be evaluated
with equal signs or just standing there {*filter*}.  That is:

These are equivalent:
  If InStr(x, y) = True Then...
  If Instr(x, y) Then...

And these are equivalent:
  If InStr(x, y) = False Then...
  If Not Instr(x, y) Then...

But these are faster:
  If InStr(x, y) = 0 Then...   [meaning False, y isn't found within x]
  If Instr(x, y) <> 0 Then...  [meaning True, y is found within x]

(The help isn't too terribly vocal about reminding us that we can use
InStr just to find out if a string occurs *anywhere* within another,
i.e., when we don't particularly care where.  But it's versatile that
way -- you can use it to pinpoint a spot or just to nod yes or no.)


------ WWW: http://www.*-*-*.com/ ~mtangard ----------------------
------------- "Life is nothing if you aren't obsessed." --John Waters
---------------------------------------------------------------------

Quote:

> Hi Jonathan,

> I'm always happy when I see your name on a reply because I know there
> will be solid useful information inside!

> Your examples below have made clear to me things about Instr that I've
> been confused about forever, even after looking at several help books.

> Thanks!

> From what you've said at the end of your post, I can change my False's
> to 0's and the macros will run faster.

> What I'm still not sure of is, sometimes I didn't put any "=" phrase
> after the "If Instr".  For example, if I have the code:

>  If Instr("string 1", "string 2") Then
>  'do x'

> what happens is, if string 2 is present, then it runs the 'do x' code.
> If string 2 is not present, 'do x' is skipped.

> So is it that the code knows that any value other than "0" should be
> considered to satisfy the "If"?

> Jerry



> >Hi JMB,

> >In addition to Jay's answer, remember that InStr returns a numeric value,
> >being zero if the second string is not in the first, and equal to the
> >character position of the first character of the first occurrence of the
> >second string in the first otherwise. Thus

> >InStr("abcd", "de") = 0
> >InStr("abcd", "cd") = 3
> >InStr("abcd", "abc") = 1
> >InStr("abcd", "a") = 1
> >InStr("abcdabcd", "de") = 0
> >InStr("abcdabcd", "da") = 4
> >InStr("abcdabcd", "cd") = 3
> >InStr("abcdabcd", "a") = 1

> >When you compare the output with False, you are forcing a type conversion,
> >which takes extra time. For practical effect comparing with False is the
> >same as comparing with zero, so you might as well do that and avoid the type
> >conversion.



Mon, 03 Nov 2003 12:10:43 GMT  
 going crazy with Instr
Thanks for your reply, Mark.

You're right about the Help.  Only by accident did I "discover" this
use of Instr where I don't care at what location the found string is!

On Wed, 16 May 2001 21:10:43 -0700, Mark Tangard

Quote:

>Jerry,

>In that case InStr is essentially being converted to a Boolean, just
>like it is when you use "= False", and places -- I would assume --
>the same kind of extra load on the processor.  The reason it looks
>like a slightly different case is because Booleans can be evaluated
>with equal signs or just standing there {*filter*}.  That is:

>These are equivalent:
>  If InStr(x, y) = True Then...
>  If Instr(x, y) Then...

>And these are equivalent:
>  If InStr(x, y) = False Then...
>  If Not Instr(x, y) Then...

>But these are faster:
>  If InStr(x, y) = 0 Then...   [meaning False, y isn't found within x]
>  If Instr(x, y) <> 0 Then...  [meaning True, y is found within x]

>(The help isn't too terribly vocal about reminding us that we can use
>InStr just to find out if a string occurs *anywhere* within another,
>i.e., when we don't particularly care where.  But it's versatile that
>way -- you can use it to pinpoint a spot or just to nod yes or no.)


>------ WWW: http://www.*-*-*.com/ ~mtangard ----------------------
>------------- "Life is nothing if you aren't obsessed." --John Waters
>---------------------------------------------------------------------


>> Hi Jonathan,

>> I'm always happy when I see your name on a reply because I know there
>> will be solid useful information inside!

>> Your examples below have made clear to me things about Instr that I've
>> been confused about forever, even after looking at several help books.

>> Thanks!

>> From what you've said at the end of your post, I can change my False's
>> to 0's and the macros will run faster.

>> What I'm still not sure of is, sometimes I didn't put any "=" phrase
>> after the "If Instr".  For example, if I have the code:

>>  If Instr("string 1", "string 2") Then
>>  'do x'

>> what happens is, if string 2 is present, then it runs the 'do x' code.
>> If string 2 is not present, 'do x' is skipped.

>> So is it that the code knows that any value other than "0" should be
>> considered to satisfy the "If"?

>> Jerry



>> >Hi JMB,

>> >In addition to Jay's answer, remember that InStr returns a numeric value,
>> >being zero if the second string is not in the first, and equal to the
>> >character position of the first character of the first occurrence of the
>> >second string in the first otherwise. Thus

>> >InStr("abcd", "de") = 0
>> >InStr("abcd", "cd") = 3
>> >InStr("abcd", "abc") = 1
>> >InStr("abcd", "a") = 1
>> >InStr("abcdabcd", "de") = 0
>> >InStr("abcdabcd", "da") = 4
>> >InStr("abcdabcd", "cd") = 3
>> >InStr("abcdabcd", "a") = 1

>> >When you compare the output with False, you are forcing a type conversion,
>> >which takes extra time. For practical effect comparing with False is the
>> >same as comparing with zero, so you might as well do that and avoid the type
>> >conversion.



Mon, 03 Nov 2003 12:13:04 GMT  
 going crazy with Instr


Quote:
> Hi Jonathan,

> I'm always happy when I see your name on a reply because I know there
> will be solid useful information inside!

> Your examples below have made clear to me things about Instr that I've
> been confused about forever, even after looking at several help books.

> Thanks!

You're welcome :-)

Quote:

> From what you've said at the end of your post, I can change my False's
> to 0's and the macros will run faster.

Yes

Quote:

> What I'm still not sure of is, sometimes I didn't put any "=" phrase
> after the "If Instr".  For example, if I have the code:

>  If Instr("string 1", "string 2") Then
>  'do x'

That is acceptable code, though as a matter of principle I am not very keen
on having code rely on implicit type conversions. Microsoft only has to
decide to change the default behaviour for your code to break when you
upgrade to the enxt version of Word. There is a big argument going on in the
VB world at the moment because Microsoft *did* change many such things in
the beta 1 of VB.NET - for instance redefining an Integer to be 32-bit
instead of 16-bit and Long as 64-bit instead of 32-bit.

Therefore, generally, I prefer not to rely on default or implicit behaviour.
(I'll freely admit I don't *always* remember to abide by that rule <g>)

If you want to have a function that just tests for the presence of one
string in another and explicitly returns a Boolean, then of course you can
write one.

Public Function IsInString(String1 as String, String2 As String) As Boolean
    IsInString = (InStr(String1, String2) > 0)
End Function

How that works is not necessarily obvious at first sight. Think about it
though, and you will realise that the comparison operator > can only return
a Boolean. Either the first number is greater than the second (True) or it
isn't (False) and that True/False value can be assigned to a variable. You
aren't limited to using comparison operators only in If-Then or Do-Loop
constructions.

Put that function in a module somewhere, and then use IsInString where you
previously used InStr. That way, you avoid the risk of implicit type
conversion (called Evil Type Coercion or ETC by some VB programmers) when
all you want to do is check the presence of one string in another.

--
Regards
Jonathan West - Word MVP
MultiLinker - Automated generation of hyperlinks in Word
Conversion to PDF & HTML
http://www.multilinker.com
Word FAQs at http://www.multilinker.com/wordfaq
Please post any follow-up in the newsgroup. I do not reply to Word questions
by email



Mon, 03 Nov 2003 16:54:36 GMT  
 going crazy with Instr

Quote:
> InStr("abcd", "de") = 0
> InStr("abcd", "cd") = 3
> InStr("abcd", "abc") = 1
> InStr("abcd", "a") = 1
> InStr("abcdabcd", "de") = 0
> InStr("abcdabcd", "da") = 4
> InStr("abcdabcd", "cd") = 3
> InStr("abcdabcd", "a") = 1

Don't forget

InStr(null, "a") = null
InStr("a", null) = null

--------------------------------------------------------------------
Jeff Hall MSc ARICS,  Microsoft HTML Help MVP
Director, Eon Commerce Ltd.
http://www.eon-commerce.com

Software available for you to evaluate before buying...
EasyHTML/Help CHM file Editor for MS Word
http://www.easyhtmlhelp.com
--------------------------------------------------------------------



Mon, 03 Nov 2003 17:24:00 GMT  
 going crazy with Instr
Thanks, Jonathan, once again for your reply and that useful hint about
changing my code so updates don't{*filter*}it up!



Quote:



>> Hi Jonathan,

>> I'm always happy when I see your name on a reply because I know there
>> will be solid useful information inside!

>> Your examples below have made clear to me things about Instr that I've
>> been confused about forever, even after looking at several help books.

>> Thanks!

>You're welcome :-)

>> From what you've said at the end of your post, I can change my False's
>> to 0's and the macros will run faster.

>Yes

>> What I'm still not sure of is, sometimes I didn't put any "=" phrase
>> after the "If Instr".  For example, if I have the code:

>>  If Instr("string 1", "string 2") Then
>>  'do x'

>That is acceptable code, though as a matter of principle I am not very keen
>on having code rely on implicit type conversions. Microsoft only has to
>decide to change the default behaviour for your code to break when you
>upgrade to the enxt version of Word. There is a big argument going on in the
>VB world at the moment because Microsoft *did* change many such things in
>the beta 1 of VB.NET - for instance redefining an Integer to be 32-bit
>instead of 16-bit and Long as 64-bit instead of 32-bit.

>Therefore, generally, I prefer not to rely on default or implicit behaviour.
>(I'll freely admit I don't *always* remember to abide by that rule <g>)

>If you want to have a function that just tests for the presence of one
>string in another and explicitly returns a Boolean, then of course you can
>write one.

>Public Function IsInString(String1 as String, String2 As String) As Boolean
>    IsInString = (InStr(String1, String2) > 0)
>End Function

>How that works is not necessarily obvious at first sight. Think about it
>though, and you will realise that the comparison operator > can only return
>a Boolean. Either the first number is greater than the second (True) or it
>isn't (False) and that True/False value can be assigned to a variable. You
>aren't limited to using comparison operators only in If-Then or Do-Loop
>constructions.

>Put that function in a module somewhere, and then use IsInString where you
>previously used InStr. That way, you avoid the risk of implicit type
>conversion (called Evil Type Coercion or ETC by some VB programmers) when
>all you want to do is check the presence of one string in another.



Tue, 04 Nov 2003 13:03:29 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. Please, save a soul from going crazy ...

2. I'm going crazy..

3. help me please ..i'm going crazy

4. Cast not valid - going crazy

5. Help Please with UploadFile---Going Crazy

6. sorry - news server went crazy - how do i modify a date field from asp

7. I'm going CRAZY with MSDataReport

8. ADO misinterprets Serial Date - I'll go crazy

9. Going crazy !!! Help please

10. HELP : I'm going crazy CORRUPTIONS CORRUPTIONS

11. ** HELP WITH vb 4.0 I am going crazy**

12. Newbe ... going CRAZY spent too much $$$

 

 
Powered by phpBB® Forum Software