#1
|
|||
|
|||
count selected checkboxes in each group
Hi everyone,
I have a Word document with 7 questions each has 6 different answers as checkboxes and a Total text field under each group of answers/ checkboxes. I need a macro to automatically count the numbers of selected checkboxes in each question and display the sum in the Total field underneath each group. I've been searching the net for over 2 days now and have yet to find a good macro to do that same job. Thanks so much for any help |
#2
|
|||
|
|||
If your structure is consistent (6 checks for each question) then the following should work. You will have to use a naming convention like "grp1Chk1, grp1Chk2, etc. and name the group totals grp1Tally, grp2Tally etc.
Code:
Sub TallyTicks() 'A basic Word macro coded by Greg Maxey Dim lngGroup As Long Dim lngIndex As Long Dim lngTally As Long lngGroup = Mid(Selection.FormFields(1).Name, 4, 1) For lngIndex = 1 To 3 If ActiveDocument.FormFields("grp" & lngGroup & "Chk" & lngIndex).CheckBox.Value = True Then lngTally = lngTally + 1 End If Next lngIndex ActiveDocument.FormFields("grp" & lngGroup & "Tally").Result = CStr(lngTally) lbl_Exit: Exit Sub End Sub |
#3
|
|||
|
|||
Thanks so much gmaxey
working great I just had to change the IngIndex to 6 instead of 3 Thanks a lot again for your kind and great help |
#4
|
|||
|
|||
Sorry about that. In my testing I only created 3 checkboxes. I meant to tell you that you run the macro on exit from each checkbox. I assume you figured that out on your own.
Best Regards, Greg Maxey War is cruelty. There is no use trying to reform it. The crueler it is, the sooner it will be over. ~ Gen William T. Sherman |
#5
|
|||
|
|||
Thank you Greg, you`ve been of great kind help.
Actually I had set the macro to run On Entry for each checkbox and it was working great, fast and correct. I changed it to run On Exit as per your advice and that made it run very slow and sort of buggy ! i.e. When I uncheck a previously checked box, the tally would not update correctly ! I changed it back to run On Entry and all fine ! If I may ask for one more thing to help me with please: I wonder if I can have either or both of the following achieved based on what the tally is for each group:
- if the total is 1 or 2, the color is green and/or the message is "a message for this score..." - if the total is 3 or more, the color is red and/or the message is "a special message for this higher score..." Thank you so much anyways |
#6
|
|||
|
|||
I see what you mean. Actually there is no real change event for the checkbox. With it running on entry it will detect a state change if you click the checkbox. However, if you then change your mind and click it again, your tally will not update. It would be best to run the macro OnEntry and OnExit.
Code:
Sub TallyTicks() 'A basic Word macro coded by Greg Maxey Dim lngGroup As Long Dim lngIndex As Long Dim lngTally As Long lngGroup = Mid(Selection.FormFields(1).Name, 4, 1) For lngIndex = 1 To 6 If ActiveDocument.FormFields("grp" & lngGroup & "Chk" & lngIndex).CheckBox.Value = True Then lngTally = lngTally + 1 End If Next lngIndex ActiveDocument.FormFields("grp" & lngGroup & "Tally").Result = CStr(lngTally) Select Case lngTally Case 1 To 2 ActiveDocument.FormFields("grp" & lngGroup & "Tally").Range.Font.ColorIndex = wdGreen MsgBox "A message for this low score." Case Is > 2 ActiveDocument.FormFields("grp" & lngGroup & "Tally").Range.Font.ColorIndex = wdRed MsgBox "A message for this higher score" Case Else ActiveDocument.FormFields("grp" & lngGroup & "Tally").Range.Font.ColorIndex = wdAuto End Select lbl_Exit: Exit Sub End Sub |
#7
|
|||
|
|||
Thanks Greg
Quote:
Thanks for the code update but there is one problem with the message box. It pops up with each check click and this isn't practical. It would be great if the message would instead display in a text field next to the Tally in each group (I created a text field called grp#Message next to each tally). This way, it would be there always and without any repeated popups. Thanks so much for your great help and patience |
#8
|
|||
|
|||
After some trials and errors, I solved the message display thing.
Thanks for all your kind help Greg |
#9
|
|||
|
|||
a8cube,
I'm sorry for not getting back to you. I got wrapped up in another problem and forgot all about this. Glad to know you got it worked out. |
Tags |
checkbox, checked boxes, count multiple |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Group Table Rows/Column (like Excel Group) | eoinymc | Word | 1 | 03-11-2014 04:51 AM |
checkboxes | smohap | Word | 1 | 06-19-2011 09:24 PM |
Excel Checkboxes | theresamille699 | Excel | 3 | 04-12-2011 08:08 PM |
Ticking Checkboxes | screid | Word | 1 | 06-08-2010 02:04 AM |
Manipulating checkboxes in XML | Ivo | Word | 0 | 12-06-2005 09:04 AM |