• Counting out time (2002 SP3)

    Author
    Topic
    #424877

    Not the Genesis track but…

    I have a document containing a table within which there are multiple occurrences of “(nn mins)” or “(n mins)” (without the quotes), where n is a number.

    They are in various cells and sometimes more than once in a cell.

    I need a macro to add up all the numbers and give me the total.

    Can anyone point me to some already written samples that do anything similar?

    TIA
    Regards
    Paul

    Viewing 0 reply threads
    Author
    Replies
    • #977061

      An Excel table might be more suitable for this purpose, but you can probably find something you can use in macropod’s Date Calculations in Word.

      • #977084

        My problem is how to go fetch all the values to add up.

        The Date Calculations stuff doesn’t seem to help in this.

        Adding the numbers up in VBA isn’t a problem, it is how I get all the numbers out of the text in the first place.

        Any help appreciated.

        • #977089

          Could you post a small sample document? Remove any sensitive information.

          • #977095

            As requested

            • #977105

              Try this:

              Sub AddTime()
              Dim lngMinutes As Long
              Dim strText As String
              Selection.HomeKey Unit:=wdStory
              With Selection.Find
              .ClearFormatting
              .Replacement.ClearFormatting
              .Text = “([0-9]{1,2} mins)”
              .Replacement.Text = “”
              .MatchWildcards = True
              .Wrap = wdFindStop
              .Forward = True
              Do While .Execute = True
              strText = Mid(Selection.Text, 2)
              lngMinutes = lngMinutes + Val(strText)
              Loop
              End With
              MsgBox lngMinutes & ” minutes”
              End Sub

              Of course, you can replace the MsgBox statement at the end with something useful.

            • #977220

              Wow thanks Hans. The MsgBox is as useful as it needs to get.

              One other thing, which I know I have asked and had resolved somewhere before.

              How does one remember where the cursor started and return to that position at the end of running the macro.

              Naively I have tried

              Set orig = Selection
              .
              .
              .
              .
              orig.Select

              But I remember that there needs to be a bit more to actually get it to function properly.

              TIA

            • #977223

              Use

              Dim orig As Range
              Set orig = Selection.Range
              .
              .
              .

              orig.Select

    Viewing 0 reply threads
    Reply To: Counting out time (2002 SP3)

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

    Your information: