cell number format

H

Helmut

I have one table in a word document. In column 3 and 7 starting from row 2 I
want to change the input 10000 to 10,000.
I found the following:

Sub format()
'
Dim i As Long, mynum As Range
With ActiveDocument.Tables(1)
For i = 2 To .Rows.Count
Set mynum = .Cell(i, 3).Range
mynum.End = mynum.End - 1
.Cell(i, 3).Range = format(mynum, "#,##0")
Next i
End With

End Sub
--------------------------
It works fine for column 3, but when I change the 3 to 7 in a second sub, it
changes the values, but stops at:

Set mynum = .Cell(i, 7).Range

===========
Is there anyway I can do both columns in one macro?
what's the code?
thanks
 
D

Doug Robbins - Word MVP

Dim i As Long, mynum As Range
With ActiveDocument.Tables(1)
For i = 2 To .Rows.Count
Set mynum = .Cell(i, 3).Range
mynum.End = mynum.End - 1
.Cell(i, 3).Range = format(mynum, "#,##0")
Set mynum = .Cell(i, 7).Range
mynum.End = mynum.End - 1
.Cell(i, 7).Range = format(mynum, "#,##0")
Next i
End With

--
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
 
H

Helmut

Hi Doug,
I get this error:
Runtime error 5941 - the requested number of the collection des not exist.

It actually executes all two columns, changing the format but then the Erro
pops up and when pressing Debug the following is highlighted:

Set mynum = .Cell(i, 7).Range

Doug, in addition, I have a SUM of the two columns and would like to
'update' the field after formatting the cells in the two columns.
what additional command do I need to 'update the field'?
thanks
Helmut
 
D

Doug Robbins - Word MVP

It works fine here. To get that format for the formula, set it in the
Insert Formula dialog.

Note that if the formula is in the last row of the able, the macro as it is
written will convert the result of the formula to text. If you don't want
that to happen, change

For i = 2 To .Rows.Count

to

For i = 2 To .Rows.Count - 1

--
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
 

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