• Adding points

    Author
    Topic
    #508134

    Hi All,

    My macro writing skills are not what they used to be and I find myself in need of a simple (?) macro.

    I have a Word document which represents a test. The number of points allocated to each question is enclosed in square brackets, so something like [3]. I can’t recall if any question goes into double digits but I’d have to look back at all my tests to double check if that’s really true. If what’s enclosed between the brackets is not numeric or it’s more than 2 digits (more likely even more than 1 digit), then that occurrence should be skipped.

    The macro I need would search thru an entire document (usually not more than 4-5 pages) and add the numbers between square brackets per the conditions above. At the end, it would display in a msg box the total number of points it found and maybe the number of questions it found.

    One caveat: if the font of the square brackets is hidden, then the number should be ignored.

    TIA

    Fred

    Viewing 11 reply threads
    Author
    Replies
    • #1590570

      You don’t need a macro for what you have described – a wildcard Find will do! Simply use:
      Find = [[0-9]{1,2}]
      with the ‘reading highlight’ option and Word will tell you how many ‘points’ there are. Similarly, for a count of the questions (which presumably all have at least a single point), simply use:
      Find = [1]
      or even an ordinary Find with:
      Find = [1]

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

    • #1590585

      Hi Paul,

      Thanks for response. A couple of things:

      – it looks like there was something lost in your response in the 3rd line (“with the and Word”)

      – I’m doing this in Word 2003, haven’t tried 2010+ yet. So don’t know if results will be different. Assume not (although I know MS changed the short cut key for CTRL+f for something else)

      – with the first Find to add points, Word did highlight the occurrences of [3] or whatever the points were throughout the doc. But it didn’t add the values. It did even ignore cases where [3] was formatted as hidden (I sometimes delete questions from the file by hiding the question in case I decide to bring it back in the future).

      – with the second Find to add the # questions, I got “The search item was not found” for both variants of your suggestion (w or w/o wildcards).

      -Lastly, while I’m not 100% sure that a macro is needed (despite some of the items above), I suggested a macro bcs I anticipate difficulties with others using the Find approach in just typing the “Find What” string (I know you could store the string and copy-paste it into the “Find What” box) and remembering to check the “Use Wildcard” box (I just forgot to do it myself when doing a 4th try with your approach). So maybe a macro is needed just for “ease of use.” Further, as one is constructing a test, adding/deleting questions and changing point values, one might want to run the “add the points” process many times. While the search string would “survive” while the Word document was open (even if closing the Find dialog), if one closes the Word doc and resumes editing later, having to type the search string again becomes error prone. Further, one has to change the string to get the # of questions and then back to get the # of points.

      So for the above, I suggested a macro.

      Fred

    • #1590662

      Oops – it should have read:
      with the ‘reading highlight’ option and Word
      Fixed.

      There should be no difference regardless of which Word version you’re using. Using the ‘reading highlight’ option should return a count of the occurrences found – on the Find dialogue itself. Similarly, if you use Find/Replace, where:
      Find = ^&
      Word will likewise tell you how many instances there are. I don’t recommend this approach, though, as it actually edits the document.

      As for the second Find, if your ‘points’ are in the [1], [2], [3] format, only the points displayed as [1] should be found. The only reason I can think of that this wouldn’t work is if your ‘points’ are not in that format or they’re created via auto-numbering.

      Once we sort out the above issues, a macro could be created.

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

    • #1590675

      Hi Paul,

      The “reading highlight” option in the F/R dialog does not, per se, exist in Word 2003. Instead, there is a check box “Highlight All Items Found In” which seems to accomplish the same thing. With the box checked, I can then select “Main Document” in the drop down below the check box (which is enabled only when the box is checked). From what I can tell, the “reading highlight” option was a button that was introduced in Word 2007 (and the check-box-plus-drop-down feature deleted, which, IMHO, was a step backwards since the 2003 feature allowed more flexibility).

      Anyway, with the box checked, the F/R dialog does tell me the # of occurrences found. However, this is not adding the # of points, which really is the more important information – having the # questions was just an extra nice-to-have to know how many points were allocated over how many questions.

      As to the [1] comment, maybe that was my misreading of your first response:

      Similarly, for a count of the questions (which presumably all have at least a single point), simply use:
      Find = [1]

      Per the bold highlighting in your response, I read that as using the find string of [1], or with the brackets, would tell me the # of questions with at least 1 point, although for the life of me I didn’t understand that. To me, that would just give me the # of occurrences of questions with 1 point. I’d have to repeat that with a change for 2, 3, 4… point questions. That is not what I’d want to do nor give to others.

      I don’t have the points set with auto-numbering and they are in the format mentioned. I’m attaching a sample file to show how the test is structured.

      Thanks.

      Fred

      • #1590677

        Anyway, with the box checked, the F/R dialog does tell me the # of occurrences found. However, this is not adding the # of points, which really is the more important information[/quote]
        But surely the # of occurrences found equals the # of points??!! If believe otherwise, perhaps you could explain why – it certainly does for your attachment.

        Per the bold highlighting in your response, I read that as using the find string of [1], or with the brackets, would tell me the # of questions with at least 1 point, although for the life of me I didn’t understand that.

        Again, surely the # of questions with at least 1 point equals the # of questions??!! That said, your attachment is confusing at best; it has no point [1] – only points [3], [4] & [2], in that order. Perhaps you could explain that, too.

        Cheers,
        Paul Edstein
        [Fmr MS MVP - Word]

    • #1590691

      Paul – I’m not sure what you’re seeing. My attachment had
      question 1 worth 3 points, so [3] appears to the left of the question # and question, dealing with “The numbers on uniforms…”
      question 2 worth 4 points dealing with “The numbers on uniforms…” (I just duplicated question 1 so I could hide a question as a test)
      question 3 worth 2 points dealing with “The amount you owe…”

      NOTE that the bold type at the top “Classify the data sets …” is not a question. It is common information or instructions that apply to the following questions.

      So I’m not sure how the 2 occurrences (forgetting question 2) shown by the F/R dialog adds to the 5 points these 2 questions are worth.

      Based on the attachment and original posting, what I was looking for was something like “You have 2 questions worth 5 points” or “You’ve allocated 5 points over 2 questions”. Recall that hidden questions are not to be counted.

      Perhaps the misunderstanding here is what gave rise to the search string in your original response of [1] ???

      Fred

    • #1590702

      Paul – I forgot to mention in my last post that my attachment was only a small part of a test. In this case, there were no 1-point questions. In other parts of an actual test, there certainly could be 1-point questions (not usually though). But there certainly could be questions worth 2, 3, 4, or 5 points. That is why I didn’t see why I’d search for [1] and [2] and [3] … since the F/R dialog using your first Find What string already gave me the number of questions. To get the number of points, I’d have to find the number of questions worth 1 point and multiply by 1; # questions worth 2 points and multiply by 2, etc and add up all the results. That’s why I originally asked for the macro.

      So I understand that searching for the #cases of at least 1 point would give me the number of questions (but I didn’t see how to do that in one step). But that is really secondary to the number of points allocated to ALL questions; the # questions is nice.

      When making up a test, a teacher wants to make sure that he/she has allocated 100 (or whatever the test is worth) points. If you’re off by a point or two, you adjust point allocations of existing questions (and want to make sure you’ve done this correctly – hence the need to re-check the point allocation). If you’re off by as many as 3, 4, or even 5 points, you add/delete questions.

      Fred

    • #1590746

      OK, it seems your requirements are quite different from what your first post suggested. Try:

      Code:
      Sub Demo()
      Application.ScreenUpdating = False
      Dim ArrPoints(), i As Long, j As Long, StrOut As String
      ReDim ArrPoints(0)
      With ActiveDocument.Range
        With .Find
          .ClearFormatting
          .Replacement.ClearFormatting
          .Text = "[[0-9]{1,2}]"
          .Replacement.Text = ""
          .Forward = True
          .Wrap = wdFindStop
          .Format = False
          .MatchWildcards = True
          .Execute
        End With
        Do While .Find.Found
          i = CLng(Replace(Replace(.Text, "]", ""), "[", ""))
          If i > UBound(ArrPoints) Then ReDim Preserve ArrPoints(i)
          ArrPoints(i) = ArrPoints(i) + 1
          .Collapse wdCollapseEnd
          .Find.Execute
        Loop
      End With
      For i = 0 To UBound(ArrPoints)
        StrOut = StrOut & vbCr & i & ": " & ArrPoints(i)
        j = j + ArrPoints(i)
      Next
      Application.ScreenUpdating = True
      MsgBox "There are " & j & " questions," & vbCr & "with point values distributed as follows:" & StrOut
      End Sub

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

    • #1590752

      Hi Paul,

      Thanks so much for the macro. My macro writing skills are not so rusty after all.

      I’m not sure what you read in my first post and I apologize if I sent you in the wrong direction. What I said in that post:

      Where to find the points for each question:

      The number of points allocated to each question is enclosed in square brackets, so something like [3]

      Conditions on what might be inside the square brackets:

      If what’s enclosed between the brackets is not numeric or it’s more than 2 digits (more likely even more than 1 digit), then that occurrence should be skipped

      And another condition:

      if the font of the square brackets is hidden, then the number should be ignored

      What the macro should do subject to above conditions:

      search thru an entire document (usually not more than 4-5 pages) and add the numbers between square brackets

      Final Display of info:

      At the end, it would display in a msg box the total number of points it found and maybe the number of questions it found

      So your macro does what I first asked for plus the distribution of points among questions.

      I was able to add a few lines to get the TOTAL number of points in the test per the original post.

      Fred

    • #1590799

      My (mis)understanding of your original post was in drawing the implication that you had something along the lines of multiple-choice questions, with ‘points’ as follows:
      Q. What is your name:
      [1] Adam
      [2] Bryan
      [3] Charles
      and that each question had a different number of ‘points’. Hence the confusion.

      To get the results without hidden content and to give a points tally, you could use:

      Code:
      Sub Demo()
      Application.ScreenUpdating = False
      Dim ArrPoints(), i As Long, j As Long, k As Long, StrOut As String
      ReDim ArrPoints(0)
      With ActiveDocument.Range
        With .Find
          .ClearFormatting
          .Replacement.ClearFormatting
          .Text = "[[0-9]{1,2}]"
          .Replacement.Text = ""
          .Forward = True
          .Wrap = wdFindStop
          .Format = True
          .Font.Hidden = False
          .MatchWildcards = True
          .Execute
        End With
        Do While .Find.Found
          i = CLng(Replace(Replace(.Text, "]", ""), "[", ""))
          If i > UBound(ArrPoints) Then ReDim Preserve ArrPoints(i)
          ArrPoints(i) = ArrPoints(i) + 1
          .Collapse wdCollapseEnd
          .Find.Execute
        Loop
      End With
      For i = 0 To UBound(ArrPoints)
        StrOut = StrOut & vbCr & i & ": " & ArrPoints(i)
        j = j + ArrPoints(i): k = k + ArrPoints(i) * i
      Next
      Application.ScreenUpdating = True
      MsgBox "There are " & j & " questions," & vbCr & _
      "with point values distributed as follows:" & _
      StrOut & vbCr & _
      "for a total point value of " & k & "."
      End Sub

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

    • #1590806

      Hi Paul – thks for the followup.

      I added the points in the Do While loop by adding i to a counter named Test_Pts. Your way of doing it in the Do i loop may be a little more efficient but I wasn’t 100% sure of using the ArrPoints matrix.

      I see you put in a Font.Hidden = False. While I understand that, my trials showed that the Find process was skipping that anyway, so I think I’m ok.

      But I also noticed you changed the .Format attribute from False to True when adding the Font.Hidden. Is there a reason for that? Why not leave it as it was (w/o the Font.Hidden even)?

      Multiple choice questions? Never!

      Fred

    • #1590820

      You need .Font.Hidden = False; otherwise the results will vary depending on whether you’re displaying hidden text. Furthermore .Font.Hidden = False won’t work without .Format = True.

      Cheers,
      Paul Edstein
      [Fmr MS MVP - Word]

    • #1590836

      Thanks again, Paul.

      In fact, when I tried the original macro with some hidden text, including a point value in brackets, but not displaying hidden text, I got an error from the macro. So I’ve changed it as you had it in your revised macro and it works fine.

      Fred

    Viewing 11 reply threads
    Reply To: Adding points

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: