Averge of Check boxes

  • Thread starter Shaneman via OfficeKB.com
  • Start date
S

Shaneman via OfficeKB.com

I have a form were users need to select a cyheckbox that correspondes to a
rating of 1 thru 4 and I need to get the average score of all.

Example:

1 2 3 4
0 X 0 0

1 2 3 4
x 0 0 0

Average = 1.5

How can I do this? Can I do this?
 
J

Jean-Guy Marcil

Shaneman via OfficeKB.com was telling us:
Shaneman via OfficeKB.com nous racontait que :
I have a form were users need to select a cyheckbox that correspondes
to a rating of 1 thru 4 and I need to get the average score of all.

Example:

1 2 3 4
0 X 0 0

1 2 3 4
x 0 0 0

Average = 1.5

How can I do this? Can I do this?

Name each series of checkbox using a pattern like:
One1, One2, One3, One4
Two1, Two2, Two3, Two4
etc.

The actual letters do not matter, as long as each name is unique and that it
finishes with the digits 1 to 4.

Then, set each textbox to calculate on exit and to call the macro called
"Calculate" on Exit.

Finally, have a textbox named "Result" and disable its option "Fill-in
enabled".

But really, it would be easier with a userform and option buttons which are
mutually exclusive....


'_______________________________________
Sub Calculate()

ActiveDocument.FormFields("Result").Result = CStr(GetAverage)

End Sub
'_______________________________________

'_______________________________________
Function GetAverage() As Single

Dim myFormField As FormField
Dim myTotal As Single
Dim myCount As Single

For Each myFormField In ActiveDocument.FormFields
If myFormField.Type = wdFieldFormCheckBox Then
If myFormField.CheckBox.Value = True Then
myTotal = myTotal + CSng(Right(myFormField.Name, 1))
End If
myCount = myCount + 1
End If
Next

GetAverage = Format((myTotal / (myCount / 4)), "#0.0")

End Function
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
S

Shaneman via OfficeKB.com

Thanks for the reply.

You said it would be easier with user form and option buttons. Can you tell
me more? I want to do this the easiest way and would appreciate any
assistance.


Jean-Guy Marcil said:
Shaneman via OfficeKB.com was telling us:
Shaneman via OfficeKB.com nous racontait que :
I have a form were users need to select a cyheckbox that correspondes
to a rating of 1 thru 4 and I need to get the average score of all.
[quoted text clipped - 10 lines]
How can I do this? Can I do this?

Name each series of checkbox using a pattern like:
One1, One2, One3, One4
Two1, Two2, Two3, Two4
etc.

The actual letters do not matter, as long as each name is unique and that it
finishes with the digits 1 to 4.

Then, set each textbox to calculate on exit and to call the macro called
"Calculate" on Exit.

Finally, have a textbox named "Result" and disable its option "Fill-in
enabled".

But really, it would be easier with a userform and option buttons which are
mutually exclusive....

'_______________________________________
Sub Calculate()

ActiveDocument.FormFields("Result").Result = CStr(GetAverage)

End Sub
'_______________________________________

'_______________________________________
Function GetAverage() As Single

Dim myFormField As FormField
Dim myTotal As Single
Dim myCount As Single

For Each myFormField In ActiveDocument.FormFields
If myFormField.Type = wdFieldFormCheckBox Then
If myFormField.CheckBox.Value = True Then
myTotal = myTotal + CSng(Right(myFormField.Name, 1))
End If
myCount = myCount + 1
End If
Next

GetAverage = Format((myTotal / (myCount / 4)), "#0.0")

End Function
'_______________________________________
 
J

Jean-Guy Marcil

Shaneman via OfficeKB.com was telling us:
Shaneman via OfficeKB.com nous racontait que :
Thanks for the reply.

You said it would be easier with user form and option buttons. Can
you tell me more? I want to do this the easiest way and would
appreciate any assistance.

It is just that with a userform you can use OptionButtons. By using the
Group property or by putting them into frames (easier with the Group
property) they become automatically mutually exclusive.
With a Word protected form you have to add code to make the checkboxes
mutually exclusive...

You would use the same approach I have already outlined, but call the code
with the Option button Change event.

If you have never done a userform, then it may be more difficult.

I just mentioned it in case you knew about using userforms but had not
considered that possibility for your project.

For more info on userform see:
http://word.mvps.org/faqs/Userforms/index.htm


--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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