Checkbox calculations in form

J

J C

Hi, I am working on a form with mulitple checkboxes and was wondering how can
I count the amount of checkboxes then calculate a percent checked?


Any help is appreciated. Thanks.

JC
 
J

Jay Freedman

Hi, I am working on a form with mulitple checkboxes and was wondering how can
I count the amount of checkboxes then calculate a percent checked?


Any help is appreciated. Thanks.

JC

Sub CheckboxPercent()
Dim TotalCheckBoxes As Long
Dim CheckedBoxes As Long
Dim Pct As Single
Dim FFld As FormField
Dim myDoc As Document

Set myDoc = ActiveDocument ' or another

TotalCheckBoxes = 4
' or call CheckboxCount like this:
' TotalCheckBoxes = CheckboxCount(myDoc)
CheckedBoxes = 0

For Each FFld In myDoc.FormFields
If FFld.Type = wdFieldFormCheckBox Then
If FFld.CheckBox.Value = True Then
CheckedBoxes = CheckedBoxes + 1
End If
End If
Next

Pct = 100# * CSng(CheckedBoxes) / CSng(TotalCheckBoxes)

MsgBox Pct & "% of the checkboxes are checked."
End Sub

' for use if you don't know in advance how many
' checkboxes there are in the document
Function CheckboxCount(myDoc As Document) As Long
Dim FFld As FormField
Dim Ct As Long
Ct = 0
For Each FFld In myDoc.FormFields
If FFld.Type = wdFieldFormCheckBox Then
Ct = Ct + 1
End If
Next

CheckboxCount = Ct
End Function
 
J

J C

Hi Jay, I should've have been a bit more specific. This form has multiple
sections with different field types (including checkboxes), and for each
section is a "Completed" checkbox for that section. So I wouldn't want this
script to count all of the checkboxes but only these "Completed" checkboxes.
How can this script be modified to include only the section "Completed"
checkboxes? If there is a better way of doing so I am all ears.

Thanks,
JC
 
J

Jay Freedman

Hi Jay, I should've have been a bit more specific. This form has multiple
sections with different field types (including checkboxes), and for each
section is a "Completed" checkbox for that section. So I wouldn't want this
script to count all of the checkboxes but only these "Completed" checkboxes.
How can this script be modified to include only the section "Completed"
checkboxes? If there is a better way of doing so I am all ears.

This can be done, but you have to tell the macro how to recognize which ones are
the "Completed" checkboxes. Do they have something specific in their names that
aren't in any other checkbox's name? Or are they formatted with a specific style
that isn't used anywhere else? Or some other unique characteristic that a macro
could use to say "this one should be counted, but that one shouldn't"?

If you don't have any such characteristic yet, probably the easiest one is to
change the names of the "Completed" boxes. When the form is unprotected,
double-click one of these checkboxes to open its Options dialog and change the
entry in the "Bookmark" box. For example, you could name them "Completed1",
"Completed2", etc. where the number is the section number.

Then the For loop in the macro can be rewritten like this:

For i = 1 To TotalCheckBoxes
Set FFld = myDoc.FormFields("Completed" & i)
If FFld.CheckBox.Value = True Then
CheckedBoxes = CheckedBoxes + 1
End If
Next i

This can be rather touchy -- if the value of TotalCheckBoxes is wrong, or if you
don't have a checkbox name for each value of i between 1 and TotalCheckBoxes,
then an error will stop the macro. It's possible to add error checking to catch
these problems; I didn't show it here to keep things simple.

Another approach is to use the For Each as in the original macro, and use a
different If statement in place of "If FFld.Type = wdFieldFormCheckBox Then" to
make sure the name is "Completed" and a number:

If FFld.Name Like "Completed#" Then

Make the same replacement in the CheckboxCount function if you use that.
 
J

J C

I tried to combine your recommendations into this but it doesn't work. For
this line entry "Set FFld = myDoc.FormFields("Check" & i)" I get an "Compile
error: Invalid or unqualified reference". Please be with me as this is the
first time I tried to do anything fancy in Word...

--------------------------------------------
Sub CheckBoxPercent()
Dim TotalCheckBoxes As Long
Dim CheckedBoxes As Long
Dim Pct As Single
Dim FFld As FormField
Dim myDoc As Document

Set myDoc = ActiveDocument

TotalCheckBoxes = 22

For i = 1 To TotalCheckBoxes
Set FFld = myDoc.FormFields("Check" & i)
If FFld.CheckBox.Value = True Then
CheckedBoxes = CheckedBoxes + 1
End If
Next i

Pct = 100# * CSng(CheckedBoxes) / CSng(TotalCheckBoxes)

.FormFields("Percentage").Result = Pct


End Sub
 
J

Jamie

Hi Jay, I am trying to use the coding you supplied to JC, but I’m not sure
how to tweek it to fit my scenario.

I’m working in Word 2003, I have a table set up (15 rows). I have a column
titled Intake. The fields in this column are check boxes (1 for each row).
I have given the check boxes in this column a new bookmark name, Intake
(e.g., Intake1, Intake2 all the way to Intake15). Like JC, I just want to
count how many checkboxes have been checked. I do not need to calculate a
percentage as JC did. My coding is as follows:

Sub IntakeCount()
Dim TotalCheckBoxes as Long
Dim CheckedBoxes as Long
Dim FFld as FormField
Dim myDoc as Document

Set myDoc = ActiveDocument

For i = 1 To TotalCheckBoxes
Set FFld = myDoc.FormFields(“Intake†& i)
If FFld.CheckBox.Value = True Then
CheckedBoxes = CheckedBoxes + 1
End If
Next i

I have the macro run On Entry in formfield, Text108. Nothing happens. I
can’t figure out what I’m missing. Any help you can give me would be
wonderful! Thanks
 
D

Doug Robbins - Word MVP

Easiest way is the run a macro with the following code, adjusting the table
index and number of the cell as required

Dim i as Long, result as Long
result = 0
With ActiveDocument
With .Tables(1)
For i = 1 to 15
If .Cell(i, 1).Range.FormFields(1).CheckBox.Value = True Then
result = result + 1
End if
Next i
End With
.Formfields("Text108").Result = result
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
 
J

Jamie

Hi Doug, I get an error message for this line

If .Cell(i, 1).Range.FormFields(1).CheckBox.Value = True Then

The error says - The requested member of the collection does not exist.
 
J

Jamie

Hi Doug, ignore my last posting. I figured out the problem, the column with
the checkboxes is column 5, so I replaced the 1 with 5 and it is working.
Thanks so much for your help!
 

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