Instr statement not working

L

Larry

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
 
D

Doug Robbins - Word MVP

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


--
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
 
J

Jay Freedman

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
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
L

Larry

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



Jay Freedman said:
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
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

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


--
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
 
S

Stefan Blom

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.

--
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
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



Jay Freedman said:
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
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
the
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top