Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-21-2015, 05:31 AM
a888 a888 is offline count selected checkboxes in each group Windows 8 count selected checkboxes in each group Office 2007
Novice
count selected checkboxes in each group
 
Join Date: Feb 2015
Posts: 12
a888 is on a distinguished road
Question 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
Reply With Quote
  #2  
Old 02-21-2015, 07:27 AM
gmaxey gmaxey is offline count selected checkboxes in each group Windows 7 32bit count selected checkboxes in each group Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 02-21-2015, 11:23 AM
a888 a888 is offline count selected checkboxes in each group Windows 8 count selected checkboxes in each group Office 2007
Novice
count selected checkboxes in each group
 
Join Date: Feb 2015
Posts: 12
a888 is on a distinguished road
Thumbs up

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
Reply With Quote
  #4  
Old 02-21-2015, 02:45 PM
gmaxey gmaxey is offline count selected checkboxes in each group Windows 7 32bit count selected checkboxes in each group Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 02-22-2015, 05:30 AM
a888 a888 is offline count selected checkboxes in each group Windows 8 count selected checkboxes in each group Office 2007
Novice
count selected checkboxes in each group
 
Join Date: Feb 2015
Posts: 12
a888 is on a distinguished road
Default

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:
  1. The format (color) of the Tally text changes.
  2. A text displaying a message accordingly in a text field.
e.g.
- 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
Reply With Quote
  #6  
Old 02-22-2015, 08:39 AM
gmaxey gmaxey is offline count selected checkboxes in each group Windows 7 32bit count selected checkboxes in each group Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #7  
Old 02-22-2015, 11:31 AM
a888 a888 is offline count selected checkboxes in each group Windows 8 count selected checkboxes in each group Office 2007
Novice
count selected checkboxes in each group
 
Join Date: Feb 2015
Posts: 12
a888 is on a distinguished road
Default

Thanks Greg

Quote:
Originally Posted by gmaxey View Post
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.
I tried run it on both Entry and Exit same time and this gave me a runtime error ! And when I leave it as it is to run OnEntry only, all is working fine ! I tested and did uncheck some boxes and the tally would update correctly without a problem !

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
Reply With Quote
  #8  
Old 02-23-2015, 10:20 PM
a888 a888 is offline count selected checkboxes in each group Windows 8 count selected checkboxes in each group Office 2007
Novice
count selected checkboxes in each group
 
Join Date: Feb 2015
Posts: 12
a888 is on a distinguished road
Default

After some trials and errors, I solved the message display thing.

Thanks for all your kind help Greg
Reply With Quote
  #9  
Old 02-24-2015, 04:44 AM
gmaxey gmaxey is offline count selected checkboxes in each group Windows 7 32bit count selected checkboxes in each group Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply

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
count selected checkboxes in each group checkboxes smohap Word 1 06-19-2011 09:24 PM
count selected checkboxes in each group 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

Other Forums: Access Forums

All times are GMT -7. The time now is 04:49 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft