If Statement wont "Then"

C

Chris

Hi Guys

I've written this code:

Sub CheckVal()
'
'CheckVal Macro written by Chris 25 October 2004 '
'Checks the value of the current balance

ActiveDocument.Tables(2).Cell(6, 4).Select
Selection.Find.ClearFormatting

With Selection.Find
.Text = "$*0.00"
.Forward = True
.Wrap = wdFindStop
.MatchWildcards = True
End With
Selection.Find.Execute
If Selection.Find = True Then

Exit Sub

Else
Application.Run macroname:="formatit"
End If

End Sub

The "Exit Sub" bit can be a skip to another macro, as long as the "Then"
works. At the moment, it keeps skipping to the "Else" if I have the If set
to True - which seems to be the opposite of what should happen as the Find
event highlights exactly what it is supposed to. What will normally appear
in the selection is "$ 0.00" (extracted from an Access Database by
someone else's macro). I know it finds the cell value, but I can't figure
why it skips the "Then".
 
H

Helmut Weber

Hi Chris,
I wonder how you can get over
If Selection.Find = True
anyway, as this should rise error 438.
If you want to know, if a search was sucessful,
i'd suggest something like this:
Sub test891()
ResetSearch
With Selection.Find
.Text = "x"
If .Execute Then
MsgBox "was found"
Else
MsgBox "was not found"
End If
End With
ResetSearch
End Sub
Public Sub ResetSearch()
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
End Sub
 
C

Chris

Hi Helmut,

Once again you have shown me some stunningly simple code (I thank you
deeply). Ultimately, 2 different macros will go where the buttons go, but it
goes like this:

Sub CheckVal()
'
'CheckVal Macro written by Chris Robson 25 October 2004 '
'With a touch of genius added by Helmut Weber
'Checks the value of the current balance
'
ActiveDocument.Tables(2).Cell(6, 4).Select 'limits the selection.

With Selection.Find
.Text = "0.00" 'To confirm a zero balance.
If .Execute Then
MsgBox "was found" 'Will call a macro.
Else
MsgBox "was not found" 'Will call a different set.
End If
End With

End Sub

Helmut, you made it a thing of beauty. It was worth staying up to 1AM to
get the answer! If you ever get over to Oz, I owe you a large stein of ice
cold beer.

The ResetSearch command wasn't recognised, but I didn't seem to need it.
I'll test it out at work (in 7 hours! I'd better get some sleep).

Cheers
Chris
 
H

Helmut Weber

Hi Chris, too much honour,
I remember times, when I couldn't find out how to
insert a 5 1/4 inch disc into a drive, because there
are 8 possibilities. As far as "resetsearch" is concernded,
I've posted some variations, according to each task.
The main purpose is, to reset all options to what an ordinary
user would expect. Difficult enough. You may think you can do
without it, until some day... ;-)
as the find-object remembers the last search. And if that was
with matchwildcards on, e.g., or an other more odd option,
find would fail, possibly.
---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
C

Chris

Hi Helmut,

I'll have to look into the "resetsearch" command a little further in your
other posts. I tested the macro today and only found one bug, which was that
it found the wrong "0.00" value. But I figure using the wdFindStop option
for Wrap may solve that. Although I would have thought that because the cell
was selected, it would only search within the cell. The great thing is, I
can use the same simple Find method for a number of other macros - it is one
for my bag of tricks.

Cheers

Chris
 

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