Word Table Calculations

S

silentpro

Can someone please help with this, I was told this can be done in VBA....

I have a '07 word doc that acts as an enrollment form.

One table has row entries with columns for a name, id, & 5 different columns
checkbox (7 cols total).
I would like to total the entries at the bottom for the ID & 5 lesson
columns.
The ID col will all have something in common, the will start with the same
first 2 letters (TX for example) & be a total of 10 char's (alpha numeric)
The 5 other lesson columns I would like to total the amount of 'ticked'
checkboxes.

The other table has a Quantity reference to the 5 columns & I would like to
take the total of a checkbox column from the table above & input it into a
cell in this other table.

All of this while auto calculating when the user changes something real
time.

If it's easier, I can try to substitute the checkboxes for a drop down list
of 0's & 1's..

Please help, I've looked all over the web to get me this answer & haven't
had any luck yet.

thanks
 
M

macropod

Hi silentpro,

Assuming the IDs are in text formfields, you could add the following macro to your document, then make it the 'on-exit' macro for
each of the ID & checkbox formfields in the table:

Sub TableUpdate()
Dim tCel As Cell
Dim tCol As Integer
Dim iVal As Integer
Dim oFld As FormField
With ActiveDocument.Tables(1)
tCol = 2
For Each tCel In .Columns(tCol).Cells
If tCel.Range.FormFields.Count > 0 Then
For Each oFld In tCel.Range.FormFields
If oFld.Type = wdFieldFormTextInput Then _
If IsNumeric(oFld.Result) = True Then iVal = iVal + 1
Next oFld
End If
Next tCel
.Columns(tCol).Cells(.Columns(tCol).Cells.Count).Range.FormFields(1).Result = iVal
For tCol = 3 To .Columns.Count
iVal = 0
For Each tCel In .Columns(tCol).Cells
If tCel.Range.FormFields.Count > 0 Then
For Each oFld In tCel.Range.FormFields
If oFld.Type = wdFieldFormCheckBox Then _
If oFld.CheckBox.Value = True Then iVal = iVal + 1
Next oFld
End If
Next tCel
.Columns(tCol).Cells(.Columns(tCol).Cells.Count).Range.FormFields(1).Result = iVal
Next tCol
End With
End Sub

Note that, as coded, the macro allows you to add/delete columns, provided there are at least 3, and you can add/delete rows,
provided the last row has the text formfields needed to hold the result of the calculations (these formfields should have the
'fill-in enabled' option disabled). Any rows to be excluded from the calculation needn't have formfields in them, or can contain
non-numeric data or different kinds of formfields - only formfields containing numbers are counted for column 2 and only checkbox
formfields are counted for the other columns.
 

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