PDA

View Full Version : Round function in Word 2003



bkirkman56
08-07-2008, 01:39 PM
I am trying to iterate through selected text in Word 2003 and if it's a number round it to a whole number.

I would love to use Excel's Round() function but I don't see how to apply
it in a Word document.

Below I posted what has been the closest I've come to a solution. Problem I am encountering is that it does not move to the next selected word when I hit my first "number" in the selected text.

VBA Code - This is in a Sub :
...
' Round numbers
MsgBox Selection.Words.Count & " words are selected"
Dim wrd As Range
Dim num As Integer
num = 0
For Each wrd In Selection.Words
If IsNumeric(wrd) And Not IsEmpty(wrd) Then
Dim str As Variant
str = Int(wrd.Text)
wrd.Text = str
num = num + 1
End If
Next wrd

MsgBox num & " numbers were rounded"
...

Sample text that is "selected" in the word document:

<Tstate000:q_mi> Q-MI0032
<Tstate001:q_mi> MICHIGAN
<Tstate003:q_mi> 2,965.0
<Tstate005:q_mi> 18
<Tstate007:q_mi> 90.951
<Tstate009:q_mi> 90.959
<Tstate011:q_mi> PUERTO RICO
<Tstate013:q_mi> 150.0
<Tstate015:q_mi> 1
<Tstate017:q_mi> 4.601
<Tstate019:q_mi> 4.944
<Tstate021:q_mi> USA
<Tstate023:q_mi> 145.0
<Tstate025:q_mi> 1
<Tstate027:q_mi> 4.448
<Tstate029:q_mi> 4.097

bkirkman56
08-07-2008, 02:16 PM
A quick update - I changed the code to this:

' Round numbers
MsgBox Selection.Words.Count & " words are selected"
Dim wrd As Range
Dim num As Integer
Dim dnum As Double

num = 0
dnum = 0#

For Each wrd In Selection.Words
If IsNumeric(wrd) And Not IsEmpty(wrd) Then
dnum = Round(wrd.Text, 0)
'wrd.Text = dnum
num = num + 1
End If
Next wrd

MsgBox num & " numbers were rounded"

I now have a new problem - when trying to get the "words" from the selection as I am I found that the collection is 'delimiting' the "words".
For instance, when I get to 2,965.0 as the code loops thru the data I see one word as "2" the next as "965" the next as "." and the next as "0".
I sure wish this could be done in Excel but unfortunately it has to be Word. How do I get the complete number from Selection.Words ?

Tommy
08-11-2008, 09:20 AM
This should work :)

Sub a()
'Round numbers
Dim mStr As String, mChk As Double
Dim mI As Long, mLineData() As String
Dim mLine() As String
mStr = Selection.Text
mLine = Split(mStr, Chr(11))
For mI = 0 To UBound(mLine, 1)
mLineData = Split(mLine(mI), " ")
If IsNumeric(mLineData(1)) Then
mChk = Val(Replace(mLineData(1), ",", ""))
mLineData(1) = IIf(mChk - Int(mChk) > 0.5, Format(Int(mChk) + 1, "#,###"), Format(Int(mChk), "#,###"))
mLine(mI) = Join(mLineData, " ")
End If
Next
Selection.Text = Join(mLine, Chr(11))
End Sub

CreganTur
08-11-2008, 01:49 PM
Wouldn't the Round function be available if you set a reference to Microsoft Excel xx.0 Object Library? (where xx.0 is whatever version number you have).

This should make Excel's exact Round Function available for you to use in Word.

Tommy
08-11-2008, 03:38 PM
All that overhead to round? IMHO I wouldn't do it, way too much for way too little.

my pennies :)