Graham Mayor

... helping to ease the lives of Microsoft Word users.


Many people access the material from this web site daily. Most just take what they want and run. That's OK, provided they are not selling on the material as their own; however if your productivity gains from the material you have used, a donation from the money you have saved would help to ensure the continued availability of this resource. Click the appropriate button above to access PayPal.

Create a fraction

Using Word's autocorrect function, certain fonts allow fractions entered in text in the format 1/2 to be corrected to use the matching font character ½. This is all very well if the font has the required characters, but you will be struggling when you get around to 35/278 for example.

The Word MVP web site offers a useful approach to creating formatted fractions involving Word's EQ field, but here I suggest an alternative approach using a macro to format any selected fraction in the format number/number. Thus the suggested 35/278 becomes 35278.

This actually looks better when used in Word than it does in the browser e.g.

Type the fraction and then with the cursor in the fraction, or immediately after the final digit, run the macro.

The macro should be run from a keyboard shortcut, a button on the QAT (Quick Access Toolbar) or a button on the Ribbon. It does not work automatically like the autocorrect function.

If you don't know what to do with the macro code see https://www.gmayor.com/installing_macro.htm   

The Macro Code

Sub FmtFraction()
Const strList As String = "0123456789/"
Dim sFraction() As String
Dim sChar As String
Dim oRng As Range
 Set oRng = Selection.Range
 oRng.MoveStartWhile strList, wdBackward
 oRng.MoveEndWhile strList
 'Check if a fraction is selected
 'and if not show a warning and exit
 If InStr(oRng.Text, "/") = False Then
  MsgBox "No fraction selected!", vbCritical, "Format Fraction"
  Exit Sub
 End If
 'Define a new slash character
 sChar = ChrW(&H2044)
 'Split the original fraction at the slash character
 sFraction = Split(oRng.Text, "/")
 'Reformat the fraction using super and subscript
 With oRng
  .Font.Superscript = True
  .Text = sFraction(0)
  .Collapse wdCollapseEnd
  .Text = sChar
  .Font.Superscript = False
  .Collapse wdCollapseEnd
  .Text = sFraction(1)
  .Font.Subscript = True
  .Collapse wdCollapseEnd
  .Font.Subscript = False
  .Select
 End With
End Sub

 

If you do not know how to employ this code, see the tutorial elsewhere on this site.

 

 

Fractions

Using Word's autocorrect function, certain fonts allow fractions entered in text in the format 1/2 to be corrected to use the matching font character ½.

This is all very well if the font has the required characters, but you will be struggling when you get around to 35/278 for example.