Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / January 2006

Tip: Looking for answers? Try searching our database.

Instr statement not working

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Larry - 31 Dec 2005 08:36 GMT
What am I doing wrong here?  I want to find out if a semicolon, comma or
colon is in the
selection, but instead, I get a "Yes" no matter what the selection
consists of.

If InStr(1, Selection.Text, ";:,", 1) = Yes Then MsgBox "yes"

Also, if I have only one character inside the quote marks in the InStr
statement, say a comma, nothing happens even if the selection includes
that character.

This does work, but it will only check one character at a time.  I
wanted to check for any one of several characters at a time.

If InStr(1, r.Text, ";", vbBinaryCompare) > 0 then msgbox "Yes"

Thanks.
Larry
Doug Robbins - Word MVP - 31 Dec 2005 09:26 GMT
The Instr() function returns a Long variant.  As a result, you should be
testing for it to return a value >0

Note however that InStr(1, Selection.Text, ";:,", 1) would only return a
value >0 if the ;:, appeared next to each other in that order in the
document (which is unlikely to be the case).

To check if one of those punctation marks appeared somewhere in the
selection, I would use something like:

Dim Flag As Long
Flag = 0
Flag = Flag + InStr(1, Selection.Text, ",", 1)
Flag = Flag + InStr(1, Selection.Text, ";", 1)
Flag = Flag + InStr(1, Selection.Text, ":", 1)
If Flag > 0 Then
   MsgBox "Yes" 'The selection contains either a , ; or :
Else
   MsgBox "No" 'The selection does not contain either a , ; or :
End If

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

> What am I doing wrong here?  I want to find out if a semicolon, comma or
> colon is in the
[quoted text clipped - 14 lines]
> Thanks.
> Larry
Jay Freedman - 31 Dec 2005 15:52 GMT
As Doug said, the Instr() function looks for the complete expression
";:," in the Selection.Text, and returns its position as a Long. But
there's an easier way to check for the existence of any one of a set
of characters, using the Like operator.

The Like operator takes a pattern and returns True or False to
indicate whether the input (Selection.Text in this case) matches the
pattern. The pattern can contain a set of characters within square
brackets, and a match occurs if the corresponding character in the
input is any one of the set.

So you can get the result you want with this code:

  If Selection.Text Like "*[;:,]*" Then MsgBox "yes"

The first asterisk will match all the characters to the left of a
semicolon, colon, or comma (if one exists). Then the set [;:,] matches
the semicolon, colon, or comma (if one exists). The second asterisk
matches all characters to the right. The only way the operator would
return False is if there is no semicolon, colon, or comma anywhere in
the Selection.Text.

--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

>The Instr() function returns a Long variant.  As a result, you should be
>testing for it to return a value >0
[quoted text clipped - 35 lines]
>> Thanks.
>> Larry
Larry - 31 Dec 2005 16:24 GMT
Thanks to Doug and Jay for the alternative ways of doing this.  I must
have imagined that there was a way of using a single InStr expression to
look for one of several characters within a string.

It's a side point, but I was still wondering, why does this code return
a "Yes" message box no matter what's in the selection, whether it has
the semicolon, colon, or comma or not?

If InStr(1, Selection.Text, ";:,", 1) = Yes Then MsgBox "yes"

Larry

> As Doug said, the Instr() function looks for the complete expression
> ";:," in the Selection.Text, and returns its position as a Long. But
[quoted text clipped - 64 lines]
> >> Thanks.
> >> Larry
Stefan Blom - 02 Jan 2006 09:36 GMT
This depends on what is stored in the variable named Yes. When Instr
doesn't find ";:," it returns the numeric value zero. If the Yes
variable is equal to zero, the comparison

If InStr(1, Selection.Text, ";:,", 1) = Yes

evaluates to True. Similarly, if the Yes variable hasn't been
explicitly defined, then it will by default be a Variant data type
with an initial value of EMPTY, which will be converted to zero when
compared to a numeric value. Again, the result of the comparison will
be True.

Signature

Stefan Blom
Microsoft Word MVP

> Thanks to Doug and Jay for the alternative ways of doing this.  I must
> have imagined that there was a way of using a single InStr expression to
[quoted text clipped - 81 lines]
> > >> Thanks.
> > >> Larry
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.