Percent formatted field makes me enter as .10

  • Thread starter Thread starter lauralee08
  • Start date Start date
L

lauralee08

I have my form field set up as a percent, in Excel I am used to just entering
"10", and it will convert to "10%" or "10.00%", but Word is changing that
same entry to "1000%". It makes me enter as ".10", which is just stupid that
I have to do that only in Word and nowhere else. Does anyone know a way to
change this so I can enter 10 and get 10%?
 
If you want to enter 10 and have it display as 10% you are going to need
perform a calculation on that field which will need vba. The following macro
run on exit from the form field in question (which should be set as text
type) will allow you to enter 10 to produce 10% and 10.5 to produce 10.5%
etc. when you tab out of that field. The macro assumes the field is
bookmarked Text1

Sub Percentage()
Dim oFld As FormFields
Dim sRes As String
Dim sDec As String
Set oFld = ActiveDocument.FormFields
sRes = oFld("Text1").Result
If InStr(1, sRes, "%") Then
sRes = replace(sRes, "%", "")
End If
If sRes <= Int(sRes) Then
sDec = "###%"
Else
sDec = "###.##%"
End If
oFld("Text1").Result = Format(sRes / 100, sDec)
End Sub= Format(sRes / 100, "###.00%")
End Sub


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

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
Lenny here:
Graham - I too have the same problem, however, when I copied your code into
my module, I started getting errors when trying to run the code. It did not
like the second to last End Sub= statement (indicating it could only be used
with comments), when I deleted it and ran the code, it did not care for the
If sRes <= Int(sres) ... at that point, I thought I would come back to the
source. I am running Word 2003. Regards
 
I am not surprised it didn't work - something odd happened there during the
transcribing of the code :(

Sub Percentage()
Dim oFld As FormFields
Dim sRes As String
Dim sDec As String
Set oFld = ActiveDocument.FormFields
sRes = oFld("Text1").Result
If InStr(1, sRes, "%") Then
sRes = Replace(sRes, "%", "")
End If
If sRes <= Int(sRes) Then
sDec = "###%"
Else
sDec = "###.##%"
End If
oFld("Text1").Result = Format(sRes / 100, sDec)
End Sub

is what it should have been. Apologies :o)


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

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
Graham: the revised code worked perfectly... a question though. When
tabbing from another form field, into the "text1" field, everythings fine as
long as you actually enter a number and tab out. If you try to tab thru the
field (not entering a number) you get a "run time error (13)". Can this be
corrected? Lenny
 
Immediately after the line sRes = oFld("Text1").Result, add this line:

If Len(sRes) = 0 Then Exit Sub

If you would rather have 0 appear in the field when you tab through it, use
this line instead:

If Len(sRes) = 0 Then sRes = "0"


--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
Jay: works like a charm! regards, Lenny

Jay Freedman said:
Immediately after the line sRes = oFld("Text1").Result, add this line:

If Len(sRes) = 0 Then Exit Sub

If you would rather have 0 appear in the field when you tab through it, use
this line instead:

If Len(sRes) = 0 Then sRes = "0"


--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 

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

Similar Threads


Back
Top