View Single Post
 
Old 08-05-2021, 11:18 AM
Yeezus Yeezus is offline Windows 10 Office 2016
Novice
 
Join Date: Aug 2021
Posts: 1
Yeezus is on a distinguished road
Default Macro command to count checkboxes and provide metrics

I have set up the developer option in the ribbon and seem to be using ActiveX controls

Five total columns that are all strings of data with the checkbox as column 1. Do have an overview that starts the table for about three rows or so. Would like to be able to show certain metrics in that area at the top of document

Number of total checkboxes + checkboxes checked
Percentage of checked / unchecked
Am I able to export results to text document? Is it possible to have multiple word document results all be stored in one master results list?

Any insight or information would be much appreciated

Below was probably best results of info when researching:

Quote:
Assuming you have checkboxes in columns (one per cell), and no other type of formfields in the table, you could use ---

Dim pTable As Word.Table
Dim pCount() As Long
Dim pFormField As Word.FormField
Dim pColumn As Long

'Get a reference to the table we're working with
Set pTable = Selection.Tables(1)

'Dimension the array to store the column counts
ReDim pCount(1 To pTable.Columns.Count)

'Iterate the formfields in the table
For Each pFormField In pTable.Range.FormFields

'If checked, increment the count for the column containing this
formfield
If pFormField.CheckBox.Value Then
pColumn = pFormField.Range.Cells(1).ColumnIndex
pCount(pColumn) = pCount(pColumn) + 1
End If

Next

'Create/update the DocVariable for each column
For pColumn = 1 to ubound(pCount)
ActiveDocument.Variables("ColCount_" & pColumn) = pCount(pColumn)
Next

'Update fields to display the new counts
with ActiveDocument
if .ProtectionType wdNoProtection then
.Unprotect
end if
.Fields.Update
.Protect wdAllowOnlyFormFields, noReset:=True
end with


Use DocVariable fields to display the totals {DocVariable ColCount_1} etc.
If the table might contain other types of formfields, you'd need to check
the FormField.Type property. The DocVariable naming convention won't work if
you have more than one table that you want to run this on. You could use the
Table.ID property to set a unique value for each table, and include that in
the DocVariable names.
Reply With Quote