How to format negative numbers in Word tables

W

Warrain

I can see how to custom format the sum of a column of numbers but not how to
format the raw numbers entered into the individual cells above. Be great if
someone could put me on the right track please.
Many thanks
 
G

Greg Maxey

Warrain,

If you are using a simple Word document with a table then you can't enter
raw numbers and have the come out formatted in real time. You can type your
numbers as raw then select the appropriate cells and format all selected
cells using a macro. Here is an example:

Sub ConvertSelectedRawNumbersInTableToCurrencyFormat()

If Not Selection.Information(wdWithInTable) Then
MsgBox "Place cursor in a table cell or select multiple cells."
Exit Sub
End If

Dim oCl As Word.Cell
Dim oRng As Range
Dim Count As Integer
For Each oCl In Selection.Cells
Set oRng = oCl.Range
oRng.End = oRng.End - 1
With oRng
If IsNumeric(oRng) Then
.Text = FormatCurrency _
(Expression:=.Text, _
NumDigitsAfterDecimal:=2, _
IncludeLeadingDigit:=vbTrue, _
UseParensForNegativeNumbers:=vbTrue)
End If
' If InStr(oRng.Text, "$") = False Then
If IsNumeric(oRng) = False Then
Count = Count + 1
End If
End With
Next oCl
If Count = 0 Then
MsgBox "Conversion complete."
End If
Selection.Collapse wdCollapseEnd
If Count = 1 Then
MsgBox "The selected cell is empty or content is not numerical.", ,
"Notice!!"
End If
If Count > 1 Then
MsgBox "" & Count & " of the selected cells are empty or content is not
numerical. Conversion complete on all selected numerical cells.", ,
"Notice!!"
End If
End Sub
 
W

Warrain

Thanks again Greg.
How can I modify your macro so that numbers in a column line up when there
is a mixture of some in brackets (negative) and some not please?
 
W

Warrain

Greg, Debug is showing the following:


Greg Maxey said:
Warrain,

If you are using a simple Word document with a table then you can't enter
raw numbers and have the come out formatted in real time. You can type your
numbers as raw then select the appropriate cells and format all selected
cells using a macro. Here is an example:

Sub ConvertSelectedRawNumbersInTableToCurrencyFormat()

If Not Selection.Information(wdWithInTable) Then
MsgBox "Place cursor in a table cell or select multiple cells."
Exit Sub
End If

Dim oCl As Word.Cell
Dim oRng As Range
Dim Count As Integer
For Each oCl In Selection.Cells
Set oRng = oCl.Range
oRng.End = oRng.End - 1
With oRng
If IsNumeric(oRng) Then
.Text = FormatCurrency _
(Expression:=.Text, _
NumDigitsAfterDecimal:=2, _
IncludeLeadingDigit:=vbTrue, _
UseParensForNegativeNumbers:=vbTrue)
End If
' If InStr(oRng.Text, "$") = False Then
If IsNumeric(oRng) = False Then
Count = Count + 1
End If
End With
Next oCl
If Count = 0 Then
MsgBox "Conversion complete."
End If
Selection.Collapse wdCollapseEnd
If Count = 1 Then
MsgBox "The selected cell is empty or content is not numerical.", ,
"Notice!!"
End If
If Count > 1 Then
MsgBox "" & Count & " of the selected cells are empty or content is not
numerical. Conversion complete on all selected numerical cells.", ,
"Notice!!"
End If
End Sub

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.
 
W

Warrain

Greg, Debug doesn't like the following line in your macro:
MsgBox "The selected cell is empty or content is not numerical.", ,
or the one following.
 
G

Greg Maxey

Warrain,

I am not ignoring you. I signe off shortly after posting and am now
preparing fo work. When you post a macro to a newsgroup, text wrapping
often messes up the longer lines. See if there is a spurious "-" in the
line or try bringing the entire string together on one line.
 
D

Doug Robbins

The following should be all on one line in the macro:

MsgBox "The selected cell is empty or content is not numerical.", ,
"Notice!!"


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
W

Warrain

thanks Doug

Doug Robbins said:
The following should be all on one line in the macro:

MsgBox "The selected cell is empty or content is not numerical.", ,
"Notice!!"


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
S

SFossett

This was very helpfull, but I just want to format as commas. How do I change
the script to either format only as commas or as currency without the $?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top