Find average from multiple spots on doc

J

Jeff

If it's possible, what is the best way to average numbers that are spread
accross the doc. I've seen how you can do formulas similar to excel, but
they won't all be in one table.
Is this possible somehow?
 
G

Graham Mayor

What do you mean by 'spread across the doc'?

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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
P

Pesach Shelnitz

Hi Jeff,

If you enclose each of the numbers in a bookmark, you can create a field
code that will display the average. For example, if you have three numbers
and you assign bookmarks called num1, num2, and num 3 to them, the field code
{=((num1 + num2 + num3)/3) } will display their average. Note that the curly
brackets ({}) enclosing the field code are not to be typed. They can created
by blocking the enclosed text and pressing Ctrl+F9, or they can be generated
by the UI.

This is a very simple example, if you have something more complex in mind,
write back with more details.
 
J

Jeff

In the mean time, I got a good handle on this. I'm just grabbing the values
of all the form fields. Now my hurdle is how is a general non-tech user
going to run the vba/macro command while the doc is in protected mode?

(FYI, here is the code I have so far to get the average and pop it in a
field):
Sub GetAvg()
Dim ff As formfield
Dim ad As Document
Dim v1, v2, v3, v4, v5, v6, v7, v8 As Integer
Dim vTot As Double
Set ad = ActiveDocument

v1 = Val(ad.FormFields("Val1").result)
v2 = Val(ad.FormFields("Val2").result)
v3 = Val(ad.FormFields("Val3").result)
v4 = Val(ad.FormFields("Val4").result)
v5 = Val(ad.FormFields("Val5").result)
v6 = Val(ad.FormFields("Val6").result)
v7 = Val(ad.FormFields("Val7").result)
v8 = Val(ad.FormFields("Val8").result)

'Get total and divide by 8
vTot = (v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8) / 8
'unlock form real fast
Call pToggleProtectDoc
ad.Bookmarks("txtOverall").Range.Text = vTot
'relock form
Call pToggleProtectDoc
End Sub

Sub pToggleProtectDoc()

' this code toggles the form protection.
' If it's on, it turns it off.
' If it's off, it turns it on. But
' rather than using the menu option,
' this code relocks the form without
' clearing the form fields (.NoReset),
' as they normally would if you
' clicked protect manually while testing!

' first I declare the word DOC
' to represent the active doc cos'
' I'm just too lazy to keep typing
' ActiveDocument and check for
' form protection

Set Doc = ActiveDocument
If Doc.ProtectionType = wdNoProtection Then

' if the form is password protected, then
' you also need to add the argument below
' Password:="myPasswordName",

Doc.Protect NoReset:=True, Type:=wdAllowOnlyFormFields
Else
Doc.Unprotect

' remember to add the password argument
' in the above line, too, if there is one.

End If


End Sub
 
G

Graham Mayor

So it's a form? You don't need a macro to achieve this, you can do it with a
conditional field construction - in this case

{ IF { =({ Val1 } + { Val2 } + { Val3 } + { Val4 } + { Val5 } + { Val6 } +
{ Val7 } + { Val8 }) / 8 } <> "!Syn*" "{ =({ Val1 } + { Val2 } + { Val3 }
+ { Val4 } + { Val5 } + { Val6 } + { Val7 } + { Val8 }) / 8 \# 0}"
"Selection includes an empty field"}

will round the result to a whole number. Change the switch \# 0 as
required - http://www.gmayor.com/formatting_word_fields.htm Check the
calculate on exit check box of at least the last field that contributes to
the calculation.

Or

You could use a calculated form field

Or

You could use a macro (if you can persuade remote users to run the macro
against their better judgement). The following is based on your code, but
writes the result in the bookmark not against it, thus not adding to the
result should the macro be run again. You could instead write the result to
another form field and the form does not require unlocking to achieve that.

..FormFields("Average").Result = vTot

The macro can be run on exit from the last formfield that contributes to the
calculation or from a toolbar button..

Sub GetAvg()
Dim ff As FormField
Dim ad As Document
Dim rAv As Range
Dim v1, v2, v3, v4, v5, v6, v7, v8 As Integer
Dim vTot As Double
Set ad = ActiveDocument
vTot = 0
With ad
v1 = Val(.FormFields("Val1").Result)
v2 = Val(.FormFields("Val2").Result)
v3 = Val(.FormFields("Val3").Result)
v4 = Val(.FormFields("Val4").Result)
v5 = Val(.FormFields("Val5").Result)
v6 = Val(.FormFields("Val6").Result)
v7 = Val(.FormFields("Val7").Result)
v8 = Val(.FormFields("Val8").Result)

'Get total and divide by 8
vTot = (v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8) / 8
'unlock form real fast
.Unprotect
Set rAv = ad.Bookmarks("txtOverall").Range
rAv = vTot
.Bookmarks.Add ("txtOverall"), rAv
.Protect wdAllowOnlyFormFields, NoReset:=True, Password:=""
End With
End Sub


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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
D

Doug Robbins - Word MVP

You do not need a macro to do this. Insert a Text Input formfield where you
want the average to be displayed and set it to a Calculation Type formfield
and then use the expression:

=(Val1 + Val2 + Val3 + Val4 + Val5 + Val6 + Val7 + Val8)/8

--
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, originally posted via msnews.microsoft.com
 
J

Jeff

Works for me! Thanks,
--
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, originally posted via msnews.microsoft.com
 

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