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
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