on error, go to next section of code

R

rex

Hello,
I'm very new to this and was trying to create some error traps. Basically,
if the text does not exists, then jump to next portion of code. In this
example, if "10 Clothing" does not exists, then jump to "20 Clothing"

Any help is appreciated

Thank you.



Sub test()
'
' test Macro
' Macro recorded 07/02/06 by rex
'
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Text = "10 Clothing"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Deposits"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Find.ClearFormatting
With Selection.Find
.Text = "reason total"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdWord, Count:=4, Extend:=wdExtend
Selection.Copy
Selection.EndKey Unit:=wdStory
Selection.Paste
Selection.TypeParagraph
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Text = "20 Clothing"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Deposits"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Find.ClearFormatting
With Selection.Find
.Text = "reason total"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdWord, Count:=4, Extend:=wdExtend
Selection.Copy
Selection.EndKey Unit:=wdStory
Selection.Paste
Selection.TypeParagraph
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
 
J

Jezebel

'Not found' is not really an error. You could use error handling for it, but
there's no need since the .Execute statement creates a condition for you
anyway. Also better not to use the Selection object.

However, your overall logic doesn't make sense. You delete "10 Closthing",
"Deposits", "Reason" etc --- It makes no difference to the deletion of
"Deposits" whether "10 Clothing" exists or not.

Why not a simple loop --- ?

For i = 1 to 4

With ActiveDocument.Content.Find
.Text = Choose(i, "10 Clothing", "Deposits", ....)
.Replacement.Text = ""
:
.Execute
End with

Next
 
R

rex

Thank your for your response
Nothing is getting deleted. What the code does is paste the totals for each
clothing department at the end of the document. Each department might have
6-13 different codes with reason totals. The code works fine as long as each
of the requested departments has a deposit. Not sure about the for next look,
I'll take a look at it though. Thanks
 
J

Jezebel

The outcome you describe is not obvious from your current code. Start with a
specification, not with the macro recorder. There are much easier ways to
accumulate totals than this!
 

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