Resting Anchor

The Anchorage

Personal website of Gregory K. Maxey, Commander USN (Retired)

Format Fractions
(A Microsoft Word Help & Tip page by Gregory K. Maxey)

DISCLAIMER/TERMS OF USE

The information, illustrations and code contained in my "Microsoft Word Tips" are provided free and without risk or obligation.

Click to acces PayPal Verification Service Click to acces PayPal Verification Service

However, the work is mine. If you use it for commercial purposes or benefit from my efforts through income earned or time saved then a donation, however small, will help to ensure the continued availability of this resource.

If you would like to donate, please use the appropriate donate button to access PayPal. Thank you!


This Microsoft Word Tips & Microsoft Word Help page simply adds to the field of tips and techniques for entering formatted fraction symbols in a Word document or applying formatting to pre-existing plain text fractions in your documents.

Word MVP Suzanne Barnhill has addressed most of the methods in detail in her Word MVP FAQ article at: Create Fraction

Word MVP Graham Mayor has posted a very effective macro for converting plain text fractions like "1/5" into a formatted fraction like 1/5 in his article: Create a Fraction

This Tips page refines the macro referenced in Suzanne's article. It expands the scope to include fractions involving variables (e.g., x/y) and variable expressions (e.g., 12a/4b). The following illustrates how some example Word text will be processed using the macro:

format fractions 1

Site Note IconNote1:
    1. Expressions with a super\sub scripted numerator\denominator can be processed however the existing super\sub scripts are not processed further. For example a2/b2 processed appears as a2b2.
     2. Be careful with dates formatted mm/yy or mm/dd as the procedure will treat dates in this format as common fractions.

VBA Script:
Public Sub FractionFormatter()
Dim lngProcessed As Long
  System.Cursor = wdCursorWait
  lngProcessed = FormatFractions
  If lngProcessed = 0 Then
    MsgBox "No fractions where processed"
  Else
    MsgBox lngProcessed & " fractions were processed."
  End If
  System.Cursor = wdCursorNormal
lbl_exit:
Exit Sub
End Sub
Public Function FormatFractions(Optional Scope As Range) As Long
Dim strSlashChr As String
Dim oRng As Word.Range
Dim oRngDivisor As Word.Range
Dim oRngDividend As Word.Range
Dim lngPosition As Long
Dim oRngSymbol As Word.Range
Dim bIsFraction As Boolean
Dim strPreceed As String
Dim strTrail As String
Dim oFldRng As Word.Range
Const strUrlText As String = "%#_|$/"

If Scope Is Nothing Then Set Scope = ActiveDocument.StoryRanges(wdMainTextStory)
strSlashChr = ChrW$(8260)
Set oRng = Scope
With oRng.Find
  .Text = "[A-Za-z0-9]{1,}^47[A-Za-z0-9]{1,}"
  .Forward = True
  .Wrap = wdFindStop
  .MatchWildcards = True
  While .Execute
    'ID the location of the division symbol "/"
    lngPosition = InStr(oRng, "/")
    'Define the range of the oRngDividend text
    Set oRngDividend = oRng.Duplicate
    oRngDividend.End = oRngDividend.Start + lngPosition - 1
    'Difine the range of the oRngDivisor text
    Set oRngDivisor = oRng.Duplicate
    oRngDivisor.Start = oRngDivisor.Start + lngPosition
    'Define the range of the division symbol
    Set oRngSymbol = oRng.Duplicate
    oRngSymbol.Start = oRngSymbol.Start + lngPosition - 1
    oRngSymbol.End = oRngSymbol.Start + 1
    bIsFraction = True 'Found text is considered a fraction unless determined otherwise.
    'Weed out false positives
    Set oFldRng = oRng.Duplicate
    oFldRng.Start = oFldRng.Start - 1
    oFldRng.End = oFldRng.End + 1
    'If found range is a part of a field it is probably a hyperlink or similar. Don't process it
    If oFldRng.Fields.Count > 0 Then
      bIsFraction = False
    End If
    'ID the preceeding and trailing chacter of the found text
    strPreceed = oRngDividend.Characters.First.Previous.Text
    strTrail = oRngDivisor.Characters.Last.Next.Text
    'Prevents processing dd/mm/yyyy formatted dates and other typical URL text
    If bIsFraction And InStr(1, strUrlText & ".?", strPreceed) > 0 Or _
      InStr(1, strUrlText, strTrail) > 0 Then
      bIsFraction = False
    End If
    'Explicitly process alpha and alphanumeric expressions.
    If bIsFraction And Not IsNumeric(oRngDividend.Text) Or bIsFraction And Not IsNumeric(oRngDivisor.Text) Then
      oRng.Select
      If MsgBox("Do you want to process this text as a fraction?", _
        vbYesNo, oRng.Text) = vbNo Then
        bIsFraction = False
      End If
    End If
    'Format the fraction
    If bIsFraction Then
      oRngDividend.Font.Superscript = True
      oRngDivisor.Font.Subscript = True
      oRngSymbol.Text = strSlashChr
      FormatFractions = FormatFractions + 1
    End If
    oRng.Collapse wdCollapseEnd
  Wend
End With
lbl_exit
Exit Function
End Function

Recipe documents normally contain common fractions in the list of ingredients. These common fractions can easily be converted using a simple find and replace procedure:

format_fractions_2
Fractional quantities plain text
format fractions 3
Fractional quantities after reformatting

Site Note IconNote: The following macro is written to process only selected text.

VBA Script:
Sub FormatRecipeFractions()
Dim arrFind As Variant
Dim arrReplace As Variant
Dim oRng As Range
Dim i As Long
  'Creat arrray of common fractions
  arrFind = Array("-1/4", "-1/2", "-3/4", "-1/3", "-2/3", _
   "-1/8", "-3/8", "-5/8", "-7/8", "1/4", "1/2", "3/4", "1/3", "2/3", _
   "1/8", "3/8", "5/8", "7/8")
  'Creat replacement array
  arrReplace = Array(ChrW(&HBC), ChrW(&HBD), ChrW(&HBE), _
    ChrW(&H2153), ChrW(&H2154), ChrW(&H215B), _
    ChrW(&H215C), ChrW(&H215D), ChrW(&H215E), _
    ChrW(&HBC), ChrW(&HBD), ChrW(&HBE), _
    ChrW(&H2153), ChrW(&H2154), ChrW(&H215B), _
    ChrW(&H215C), ChrW(&H215D), ChrW(&H215E))
  Set oRng = Selection.Range
  For i = LBound(arrFind) To UBound(arrFind)
    With oRng.Find
      .Text = arrFind(i)
      .Replacement.Text = arrReplace(i)
      .Forward = True
      .Wrap = wdFindStop
      .Execute Replace:=wdReplaceAll
    End With
  Next i
lbl_Exit:
Exit Sub
End Sub

Site Note icon See: Installing Macros for instructions on how to set up and use the macros provided in this Microsoft Word Help & Microsoft Word Tips page.

That's it! I hope you have found this tips page useful and informative.

PAYMENTS/DONATIONS

Click to acces PayPal Verification Service Click to acces PayPal Verification Service

Do you want to make a payment for consulting work or donate to help support this site?

PayPal is a safe, easy way to pay online.

Use the appropriate currency "Donate" button to make a payment or donation.


Search my site or the web using Google Search Engine

Google Search Logo

Or

JustAnswerAsk a Word Expert OnlineSubmit