Need Help With Find

J

JBNewsGroup

Hi,

I am working with WORD2000 VBA.

I am stepping through each item in a "master" document and updating tables
in a second document. In the "master" document there is a number *1. The
table that is being updated has values like *1047A, *1064, *1077, etc.. The
goal is to find an exact match, or not, for *1. Executing the Find I hit
each table value that has a *1xxx. The following code is in a Function
procedure:

-- set up table range --

With SearchRange
.Forward = True
.MatchCase = True
.MatchWholeWord = True
.MatchWildCards = False

SearchComplete = False
Do
.Execute FindText:=SearchText
If .Found then
If (SearchText = actual cell text without terminators) Then
RowNo = row number found by Execute
SearchComplete = True
End If
Else
RowNo = 0
SearchComplete = True
End If
Loop Until SearchComplete
End With

--- other code ---

How do I perform an Exact Match? Is the Loop the only way to do it or am I
missing something?
I also tried using the Selection object but it seems that I have the same
problem.

Any help or suggestions is greatly appreciated. I would like to get rid of
the Loop if possible as the table data can change and there may be "tons" of
data that could have a hit (the "master" can also change).

Thanks in advance.

Jerry Bodoff
 
H

Helmut Weber

Hi Jerry,
this is the way I usually search a range,
but as I don't quite understand what you are heading for,
some kind of loop is required anyway,
an while ... wend is a loop, too.

Sub test8766()
Dim oRng As Range
Set oRng = ActiveDocument.Tables(1).Range
ResetSearch
With oRng.Find
.Text = "*[0-9]{4}"
.MatchWildcards = True
While .Execute
MsgBox oRng.Text
Wend
End With
ResetSearch
End Sub

Public Sub ResetSearch()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
' plus some more if needed
.Execute
End With
End Sub

Note that .Text = "*[0-9]{4}" does not
regard whether a cell starts with the text
to be searched for or ends with that text,
or whether the text is found several times
in one cell.

Or do you want to stop the search, as soon as
one (1) match in a table was found and procede
with the next table?

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
J

JBNewsGroup

Hi Helmut,

Thanks for your reply. Wildcards are not valid for the search, the Match
must be exact. So I guess the the code I now have in place is the way to
go. It is too bad that MS did not have a "MatchExact" property. They gave
everything else.

Once again thanks.

Jerry Bodoff
 
J

Jay Freedman

Hi Jerry.

A wildcard search can still do exact matches, if you can pin down the
conditions well enough. Can I assume that the *1 you're looking for is
always followed by a space or punctuation? The wildcard zoo includes codes
for "start of word" and "end of word", so you may be able to use the
criterion

.Text = "\*1>"

The asterisk has to be escaped with the backslash to look for an actual
asterisk, and the > means "end of word".

--
Regards,
Jay Freedman
Microsoft Word MVP
Hi Helmut,

Thanks for your reply. Wildcards are not valid for the search, the
Match must be exact. So I guess the the code I now have in place is
the way to go. It is too bad that MS did not have a "MatchExact"
property. They gave everything else.

Once again thanks.

Jerry Bodoff
Helmut Weber said:
Hi Jerry,
this is the way I usually search a range,
but as I don't quite understand what you are heading for,
some kind of loop is required anyway,
an while ... wend is a loop, too.

Sub test8766()
Dim oRng As Range
Set oRng = ActiveDocument.Tables(1).Range
ResetSearch
With oRng.Find
.Text = "*[0-9]{4}"
.MatchWildcards = True
While .Execute
MsgBox oRng.Text
Wend
End With
ResetSearch
End Sub

Public Sub ResetSearch()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
' plus some more if needed
.Execute
End With
End Sub

Note that .Text = "*[0-9]{4}" does not
regard whether a cell starts with the text
to be searched for or ends with that text,
or whether the text is found several times
in one cell.

Or do you want to stop the search, as soon as
one (1) match in a table was found and procede
with the next table?

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
 
J

JBNewsGroup

Hi Jay,

The *1 is it. There is no punctuation or space after it. Also, there may,
or may not be, an * preceding the data and all data is variable length
alpha-numeric. I will try the wild card approach. I am still a little
sketchy using all the wild card options but I will check them out and see
what else there is. I am assuming that the end of word will work for table
cells. Thanks for replying and for the wild card hint.

Jerry Bodoff
Jay Freedman said:
Hi Jerry.

A wildcard search can still do exact matches, if you can pin down the
conditions well enough. Can I assume that the *1 you're looking for is
always followed by a space or punctuation? The wildcard zoo includes codes
for "start of word" and "end of word", so you may be able to use the
criterion

.Text = "\*1>"

The asterisk has to be escaped with the backslash to look for an actual
asterisk, and the > means "end of word".

--
Regards,
Jay Freedman
Microsoft Word MVP
Hi Helmut,

Thanks for your reply. Wildcards are not valid for the search, the
Match must be exact. So I guess the the code I now have in place is
the way to go. It is too bad that MS did not have a "MatchExact"
property. They gave everything else.

Once again thanks.

Jerry Bodoff
Helmut Weber said:
Hi Jerry,
this is the way I usually search a range,
but as I don't quite understand what you are heading for,
some kind of loop is required anyway,
an while ... wend is a loop, too.

Sub test8766()
Dim oRng As Range
Set oRng = ActiveDocument.Tables(1).Range
ResetSearch
With oRng.Find
.Text = "*[0-9]{4}"
.MatchWildcards = True
While .Execute
MsgBox oRng.Text
Wend
End With
ResetSearch
End Sub

Public Sub ResetSearch()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
' plus some more if needed
.Execute
End With
End Sub

Note that .Text = "*[0-9]{4}" does not
regard whether a cell starts with the text
to be searched for or ends with that text,
or whether the text is found several times
in one cell.

Or do you want to stop the search, as soon as
one (1) match in a table was found and procede
with the next table?

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
 
J

JBNewsGroup

Hi Jay,

I re-read the MVP article "Finding and replacing characters using
wildcards". It turns out that the wildcard string you suggested works like a
charm. I only have to worry about the first * as the data has been validated
for only one * at the beginning, if any at all. I finally got rid of the
loop that was slowing me down (1 to 3 tables may have to be searched).

Once again thanks for your help.

Jerry B

Jay Freedman said:
Hi Jerry.

A wildcard search can still do exact matches, if you can pin down the
conditions well enough. Can I assume that the *1 you're looking for is
always followed by a space or punctuation? The wildcard zoo includes codes
for "start of word" and "end of word", so you may be able to use the
criterion

.Text = "\*1>"

The asterisk has to be escaped with the backslash to look for an actual
asterisk, and the > means "end of word".

--
Regards,
Jay Freedman
Microsoft Word MVP
Hi Helmut,

Thanks for your reply. Wildcards are not valid for the search, the
Match must be exact. So I guess the the code I now have in place is
the way to go. It is too bad that MS did not have a "MatchExact"
property. They gave everything else.

Once again thanks.

Jerry Bodoff
Helmut Weber said:
Hi Jerry,
this is the way I usually search a range,
but as I don't quite understand what you are heading for,
some kind of loop is required anyway,
an while ... wend is a loop, too.

Sub test8766()
Dim oRng As Range
Set oRng = ActiveDocument.Tables(1).Range
ResetSearch
With oRng.Find
.Text = "*[0-9]{4}"
.MatchWildcards = True
While .Execute
MsgBox oRng.Text
Wend
End With
ResetSearch
End Sub

Public Sub ResetSearch()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
' plus some more if needed
.Execute
End With
End Sub

Note that .Text = "*[0-9]{4}" does not
regard whether a cell starts with the text
to be searched for or ends with that text,
or whether the text is found several times
in one cell.

Or do you want to stop the search, as soon as
one (1) match in a table was found and procede
with the next table?

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
 

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