Help with wildcard search, please?

E

Ed

I'm trying to use Find (Ctrl+F) with wildcards to find a certain string
ending in a number between 60 and 81. I tried
(App A, Item No. )([60-81]{2})
but it found "App A, Item No. 10". I looked on the Word FAQ page for
Wildcards, but couldn't find how to restrict the number to "more than this
but less than that".

Can this be done with Find, or do I need to go into VBA?

Ed
 
J

Jay Freedman

Hi Ed,

Yes, you're going to need VBA. The Find mechanism itself can't do
comparisons of the values of digits. You need something like this:

Dim myRange As Range
Dim myVal As Long

Set myRange = ActiveDocument.Range
FoundIt = False

With myRange.Find
.MatchWildcards = True
.Text = "App A, Item No. ([0-9]{2})"
.Forward = True
.Wrap = wdFindStop
Do While .Execute
myVal = Val(Right$(myRange.Text, 2))
If (60 <= myVal) And (myVal <= 81) Then
myRange.Select ' or do something else
Exit Do
End If
Loop
End With

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
K

Klaus Linke

Ed said:
I'm trying to use Find (Ctrl+F) with wildcards to find a certain string
ending in a number between 60 and 81. I tried
(App A, Item No. )([60-81]{2})
but it found "App A, Item No. 10". I looked on the Word FAQ page for
Wildcards, but couldn't find how to restrict the number to "more than this
but less than that".

Can this be done with Find, or do I need to go into VBA?


Hi Ed,

Wildcards deal with patterns, and don't have any built-in concept of
numbers. You probably need VBA.

You could use a wildcard search first to find numbers between 60 and 89
([6-8][0-9]), and then you'd need to check the number (last two characters
in the selection) with VBA:

Select Case Val(Right(Selection.Text,2))
Case 60 to 81
' match
End Select

For wildcards to work in this case, you would need an "OR" operator, which
does not exist in Word wildcards:
[6][0-9] OR [7][0-9] OR [8][0-1]

Regards,
Klaus
 
G

Greg Maxey

I think the closet you can get would be finding 60-89.
App A, Item No. [6-8][0-9][!0-9]

I assumed you wouldn't want to find part of:
App A, Item No. 627

Try:

Sub ScratchMacro()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "App A, Item No. [6-8][0-9][!0-9]"
.MatchWildcards = True
While .Execute
If CLng(Mid(oRng, 17, 2)) < 90 Then
MsgBox oRng.Text
End If
Wend
End With
End Sub
 
G

Greg Maxey

I think the closet you can get would be finding 60-89.
App A, Item No. [6-8][0-9][!0-9]

I assumed you wouldn't want to find part of:
App A, Item No. 627

Try:

Sub ScratchMacro()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "App A, Item No. [6-8][0-9][!0-9]"
.MatchWildcards = True
While .Execute
If CLng(Mid(oRng, 17, 2)) < 90 Then
MsgBox oRng.Text
End If
Wend
End With
End Sub
 
E

Ed

Huge thanks to all of you Jay, Klaus, and Greg. I really appreciate the
assistance and the codes.

Ed
 
G

Greg Maxey

Jay,

Are there advantages to Val(someString) over Clng(someString)?

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Jay Freedman said:
Hi Ed,

Yes, you're going to need VBA. The Find mechanism itself can't do
comparisons of the values of digits. You need something like this:

Dim myRange As Range
Dim myVal As Long

Set myRange = ActiveDocument.Range
FoundIt = False

With myRange.Find
.MatchWildcards = True
.Text = "App A, Item No. ([0-9]{2})"
.Forward = True
.Wrap = wdFindStop
Do While .Execute
myVal = Val(Right$(myRange.Text, 2))
If (60 <= myVal) And (myVal <= 81) Then
myRange.Select ' or do something else
Exit Do
End If
Loop
End With

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup
so all may benefit.
I'm trying to use Find (Ctrl+F) with wildcards to find a certain
string ending in a number between 60 and 81. I tried
(App A, Item No. )([60-81]{2})
but it found "App A, Item No. 10". I looked on the Word FAQ page for
Wildcards, but couldn't find how to restrict the number to "more than
this but less than that".

Can this be done with Find, or do I need to go into VBA?

Ed
 
J

Jay Freedman

It's mostly that Val is stuck in my memory from about 30 years of use,
and CLng is much more recent. You know the short-term memory is the
first to go. (My wife calls hers "momnesia".)

In most cases, if you assign the result of Val to a Long variable, the
result is identical to what you get from CLng, including the rounding
of non-integers, and the results will compare as equal.

There are some subtle differences, though.

- If the string contains anything other than digits and a decimal
point, Val will evaluate the start of the string up to (but not
including) the non-numeric character. It will also strip out and
ignore spaces, tabs, and line feeds. In the same situations, CLng will
throw a type mismatch error, which needs to be trapped with On Error.

This is not to say that Val is always superior. If you don't carefully
validate the input, Val can give you a number that looks reasonable
but is wrong, where CLng would clearly say "this is garbage".

- Both functions will accept arguments in hexadecimal notation. But
where CLng interprets all hex arguments as positive numbers, Val
interprets 4-digit hex numbers larger than 7FFF as negative numbers
(because the highest-order bit is considered to be a sign bit). You
won't run into this one very often!

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

Jay,

Are there advantages to Val(someString) over Clng(someString)?

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Jay Freedman said:
Hi Ed,

Yes, you're going to need VBA. The Find mechanism itself can't do
comparisons of the values of digits. You need something like this:

Dim myRange As Range
Dim myVal As Long

Set myRange = ActiveDocument.Range
FoundIt = False

With myRange.Find
.MatchWildcards = True
.Text = "App A, Item No. ([0-9]{2})"
.Forward = True
.Wrap = wdFindStop
Do While .Execute
myVal = Val(Right$(myRange.Text, 2))
If (60 <= myVal) And (myVal <= 81) Then
myRange.Select ' or do something else
Exit Do
End If
Loop
End With

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup
so all may benefit.
I'm trying to use Find (Ctrl+F) with wildcards to find a certain
string ending in a number between 60 and 81. I tried
(App A, Item No. )([60-81]{2})
but it found "App A, Item No. 10". I looked on the Word FAQ page for
Wildcards, but couldn't find how to restrict the number to "more than
this but less than that".

Can this be done with Find, or do I need to go into VBA?

Ed
 
G

Greg Maxey

Thanks Jay.

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.


Jay said:
It's mostly that Val is stuck in my memory from about 30 years of use,
and CLng is much more recent. You know the short-term memory is the
first to go. (My wife calls hers "momnesia".)

In most cases, if you assign the result of Val to a Long variable, the
result is identical to what you get from CLng, including the rounding
of non-integers, and the results will compare as equal.

There are some subtle differences, though.

- If the string contains anything other than digits and a decimal
point, Val will evaluate the start of the string up to (but not
including) the non-numeric character. It will also strip out and
ignore spaces, tabs, and line feeds. In the same situations, CLng will
throw a type mismatch error, which needs to be trapped with On Error.

This is not to say that Val is always superior. If you don't carefully
validate the input, Val can give you a number that looks reasonable
but is wrong, where CLng would clearly say "this is garbage".

- Both functions will accept arguments in hexadecimal notation. But
where CLng interprets all hex arguments as positive numbers, Val
interprets 4-digit hex numbers larger than 7FFF as negative numbers
(because the highest-order bit is considered to be a sign bit). You
won't run into this one very often!

Jay,

Are there advantages to Val(someString) over Clng(someString)?

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Jay Freedman said:
Hi Ed,

Yes, you're going to need VBA. The Find mechanism itself can't do
comparisons of the values of digits. You need something like this:

Dim myRange As Range
Dim myVal As Long

Set myRange = ActiveDocument.Range
FoundIt = False

With myRange.Find
.MatchWildcards = True
.Text = "App A, Item No. ([0-9]{2})"
.Forward = True
.Wrap = wdFindStop
Do While .Execute
myVal = Val(Right$(myRange.Text, 2))
If (60 <= myVal) And (myVal <= 81) Then
myRange.Select ' or do something else
Exit Do
End If
Loop
End With

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

Ed wrote:
I'm trying to use Find (Ctrl+F) with wildcards to find a certain
string ending in a number between 60 and 81. I tried
(App A, Item No. )([60-81]{2})
but it found "App A, Item No. 10". I looked on the Word FAQ page
for Wildcards, but couldn't find how to restrict the number to
"more than this but less than that".

Can this be done with Find, or do I need to go into VBA?

Ed
 

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