Adding commas to a number

C

cyberdude

Hi,

I have some numbers which are bigger than 999 in value. For example,
these numbers are 1000, 13457 and 2345.67. I need to add commas to
these numbers at the interval of every three digits starting from the
last digit in the backward direction.

For example, if I am given 13457, I need to convert it to 13,457. If
I am given 2345.67, I need to convert it to 2,345.67, if it is
1234567.89, I need to turn it to 1,234,567.89 and it if is 1234567, it
becomes 1,234,567.

Could someone tell me how to do this? Thank you.

Mike
 
G

Graham Mayor

Try

Dim sText As String
With Selection
.HomeKey Unit:=wdStory
With .Find
.ClearFormatting
.Replacement.ClearFormatting
Do While .Execute(findText:="[0-9]{4,}", _
MatchWildcards:=True, Wrap:=wdFindStop, _
Forward:=True) = True
sText = Selection.Range
sText = Format(sText, "###,###,##0")
Selection.TypeText sText
Loop
End With
End With


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
M

macropod

Hi cyberdude,

Here;s one way:
Sub Demo()
If IsNumeric(Selection) Then
If UBound(Split(Selection, ".")) = 0 Then
MsgBox Format(Selection, "#,##0")
Else
MsgBox Format(Selection, "#,##0.") & Split(Selection, ".")(1)
End If
End If
End Sub
 
M

macropod

Correction:
Sub Demo()
If IsNumeric(Selection) Then
If UBound(Split(Selection, ".")) = 0 Then
MsgBox Format(Selection, "#,##0")
Else
MsgBox Format(Split(Selection, ".")(0), "#,##0.") & Split(Selection, ".")(1)
End If
End If
End Sub

--
Cheers
macropod
[MVP - Microsoft Word]


macropod said:
Hi cyberdude,

Here;s one way:
Sub Demo()
If IsNumeric(Selection) Then
If UBound(Split(Selection, ".")) = 0 Then
MsgBox Format(Selection, "#,##0")
Else
MsgBox Format(Selection, "#,##0.") & Split(Selection, ".")(1)
End If
End If
End Sub

--
Cheers
macropod
[MVP - Microsoft Word]


cyberdude said:
Hi,

I have some numbers which are bigger than 999 in value. For example,
these numbers are 1000, 13457 and 2345.67. I need to add commas to
these numbers at the interval of every three digits starting from the
last digit in the backward direction.

For example, if I am given 13457, I need to convert it to 13,457. If
I am given 2345.67, I need to convert it to 2,345.67, if it is
1234567.89, I need to turn it to 1,234,567.89 and it if is 1234567, it
becomes 1,234,567.

Could someone tell me how to do this? Thank you.

Mike
 

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