Microsoft Office Tutorials and References
In Depth Information
Tables: Formatting Numbers in a Selection
View the Appendix to learn how to store this procedure
in a Standard module.
Option explicit¶
' * * * * *¶
Const NumberFormat As String = "$ #,0.00"¶
' * * * * *¶
Sub FormatNumbersInTableSelection()¶
'Variable declarations¶
Dim tbl As Word.Table¶
Dim cel As Word.Cell¶
Dim rng As Word.Range¶
Dim fld As Word.Field¶
Dim sCellContent As String¶
Dim sFieldCode As String¶
'Make sure the selection is in a table¶
If Selection.Information(wdWithInTable) = True Then¶
'Loop through the cells in the selection¶
For Each cel In Selection.Cells¶
Set rng = cel.Range¶
sCellContent = TrimCellText(rng.Text)¶
'Only proceed if a numerical value; ignore text¶
If IsNumeric(sCellContent) Then¶
If rng.Fields.Count = 0 Then¶
rng.Text = Format(sCellContent, _¶
NumberFormat)¶
Else¶
Set fld = rng.Fields(1)¶
sFieldCode = fld.Code.Text¶
'If the number is generated by a field¶
'check that it doesn't already contain¶
'a number format¶
If InStr(sFieldCode, "\#") = 0 Then¶
fld.Code.Text = sFieldCode & "\# " & _¶
Chr$(34) & NumberFormat & Chr$(34) & " "¶
fld.Update¶
End If¶
End If¶
End If¶
Next cel¶
End If¶
End Sub¶
' * * * * *¶
Function TrimCellText(s As String) As String¶
'Remove end-of-cell markers¶
TrimCellText = Left(s, Len(s) - 2)¶
End Function¶
Wrd
Set the number format you want to use for the Const NumberFormat value.
Search JabSto ::




Custom Search