Hi Lisa,
There's no auto-formatting, so anything you do (short of using formfields and protecting the document for forms) won't result in
numbers that are input into the table being auto-formatted. With that proviso, try the following macro:
Sub NumberFormatter()
Application.ScreenUpdating = False
Dim oCel As Cell, RngCel As Range, strBM As String, strChr, strCurr As String, i As Integer
'currency symbols
strCurr = "$,€,£,¥"
With Selection
'check that the selection is in a table
If .Information(wdWithInTable) = True Then
'check each cell in the table
For Each oCel In .Tables(1).Range.Cells
'get each cell's contents, excluding the cell marker
Set RngCel = oCel.Range
RngCel.End = RngCel.End - 1
'check for are any fields in the cell
If RngCel.Fields.Count = 0 Then
'clear the temporary bookmark store
strBM = ""
' check for any bookmarks pertaining to the cell
If RngCel.Bookmarks.Count > 0 Then
'capture the first bookmark that exactly matches this cell
If RngCel.Bookmarks(1).Range = RngCel Then strBM = RngCel.Bookmarks(1).Name
End If
'check that the cell contains a number - this includes currency in a format
'matching the system's regional settings
If IsNumeric(RngCel.Text) = True Then
'skip over percentages
If Right(RngCel.Text, 1) <> "%" Then
'test whether the first character is the local currency symbol
strChr = Left(RngCel.Text, 1)
If Not IsNumeric(strChr) Then
'currency symbol found, so reformat the value with that symbol
RngCel.Text = strChr & Format(RngCel.Text, "#,##0.00")
Else
'currency symbol not found, so reformat the value
RngCel.Text = Format(RngCel.Text, "#,##0.00")
End If
End If
Else
'check for other currencies in cells that aren't automatically assessed as values
For i = 0 To UBound(Split(strCurr, ","))
'test whether the first character is a currency symbol
If Left(RngCel.Text, 1) = Split(strCurr, ",")(i) Then
'store the cell text, minus the currency symbol
strChr = Replace(RngCel.Text, Split(strCurr, ",")(i), "")
'check that the rest of the cell contains a number
If IsNumeric(strChr) = True Then
'currency symbol found, so reformat the value with that symbol
RngCel.Text = Split(strCurr, ",")(i) & Format(strChr, "#,##0.00")
End If
End If
Next
End If
'restore the bookmark if there was one
If strBM <> "" Then ActiveDocument.Bookmarks.Add strBM, RngCel
End If
Next
'update any fields in the table
.Fields.Update
End If
End With
'update fields elsewhere in the body of the document
ActiveDocument.PrintPreview
ActiveDocument.ClosePrintPreview
Application.ScreenUpdating = True
End Sub
As coded, the macro will preserve a:
. bookmark applied to any cell value (eg for cross-referencing purposes)
. field-coded formulae
. cross-references, and
leaves values formatted as percentages, dates & times alone. Currency values and all other values are converted with a comma for
a thousands separator and a period for a decimal separator. If you have other currency forms, you can simply add them to the
'strCurr' variable. Decimals are to two places. Cross-references within the table and the body of the document generally are also
updated.
--
Cheers
macropod
[Microsoft MVP - Word]
Lisa said:
I have a Word 2007 table with two columns that contain numbers. My client
wants these numbers to reflect one decimal place. This document is fielded to
serveral locations and I do not wish to involve Excel in "importing a table".
There has to be a simple way to accomplish this. Maybe a macro?