Error Handling

G

Greg Maxey

I am working on a macro to refomat fractions. Part of the process is to
check the character before and the character after the range for a "/"
character. I know that if my range start = the active document range start
then an error will be generated. I am not very proficient or comfortable
with Error Handling. Please have a look at the method I am using to handle
this error and let me know if I have done it the way it should be done.
Thanks.

Note (this is just a part of the larger code so you can see how the error
occurs and how it is handled)

Sub FormatFraction()

Dim OrigFrac As String
Dim Numerator As String, Denominator As String
Dim NewSlashChar As String
Dim SlashPos As Integer
Dim fractionRng As Range

'Type and select 4/5 at the start of a new blank document
Set fractionRng = Selection.Range

NewSlashChar = ChrW(&H2044)
OrigFrac = fractionRng
SlashPos = InStr(OrigFrac, "/")
Numerator = Left(OrigFrac, SlashPos - 1)
Denominator = Right(OrigFrac, Len(OrigFrac) - SlashPos)
'Skip improper fractions or checksum format e.g., 123/9
If Val(Numerator) > Val(Denominator) Then
Exit Sub
End If
'skip date formats e.g., 12/31/1958
With fractionRng
On Error GoTo Handler
'Error is generated when fractionRng.Start = ActiveDocument.Range.Start
If .Characters.First.Previous = "/" Or _
.Characters.Last.Next = "/" Then
Exit Sub
End If
End With
Continue:
fractionRng.Font.Superscript = True
fractionRng = Numerator
fractionRng.Collapse Direction:=wdCollapseEnd
fractionRng = NewSlashChar
fractionRng.Font.Superscript = False
fractionRng.Collapse Direction:=wdCollapseEnd
fractionRng = Denominator
fractionRng.Font.Subscript = True
fractionRng.Collapse Direction:=wdCollapseEnd
fractionRng.Font.Subscript = False
Exit Sub
Handler:
Err.Clear
Resume Continue
End Sub
 
J

Jezebel

Technically it's OK as far as it goes. But what happens if you get some
other error in this code? Perhaps not very likely, but ANY error will jump
into your error-handler. If you wanted to be precise you could do something
like

ExitPoint:
Exit Sub


Handler:
If err.Number = 91 then
resume Continue
end if
msgbox Err.Description
Resume ExitPoint

You don't need the Err.Clear -- The error object is cleared by the Resume
statement. You might also want to introduce some discipline in the line
labels you use. These have to be unique across the project. I prefix them
with the name of the function as a quick way to guarantee that.



In this particular case, the 'error' condition is easy to test for anyway,
so your code would be more readable with normal testing ---

If not (.Characters.First.Previous is nothing or .Characters.Last.Next is
nothing) then
....
 
G

Greg

Jezebel,

I am not sure that I understand your question or suggestion.

I meant to say, "Thanks for your help. It sure beats pounding my nose
against the keyboard until it bleeds." The "Thanks" is genuine, the
second part is a reference, made lightly, to a tip that you offered
another user a while ago.
 
J

Jezebel

The response was equally light-hearted. I was just playing on the ambiguity
in the original -- most people take 'it' to refer to the nose, but
syntactically it could equally be the keyboard that bleeds. Happy to help,
anyway.
 
G

Greg Maxey

Jezebel,

Ok. Just wanted to be sure that you weren't hoping I would give myself a
bloody nose over my politics ;-).
 

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