Results 1 to 8 of 8
  1. #1
    3 Star Lounger
    Join Date
    Sep 2004
    Location
    Portsmouth, Hampshire, England
    Posts
    200
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Counting out time (2002 SP3)

    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

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Counting out time (2002 SP3)

    An Excel table might be more suitable for this purpose, but you can probably find something you can use in macropod's <!post=Date Calculations in Word,249902>Date Calculations in Word<!/post>.

  3. #3
    3 Star Lounger
    Join Date
    Sep 2004
    Location
    Portsmouth, Hampshire, England
    Posts
    200
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Counting out time (2002 SP3)

    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.

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Counting out time (2002 SP3)

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

  5. #5
    3 Star Lounger
    Join Date
    Sep 2004
    Location
    Portsmouth, Hampshire, England
    Posts
    200
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Counting out time (2002 SP3)

    As requested
    Attached Files Attached Files

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Counting out time (2002 SP3)

    Try this:

    Sub AddTime()
    Dim lngMinutes As Long
    Dim strText As String
    Selection.HomeKey Unit:=wdStory
    With Selection.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "(<!t>[0-9]<!/t>{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.

  7. #7
    3 Star Lounger
    Join Date
    Sep 2004
    Location
    Portsmouth, Hampshire, England
    Posts
    200
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Counting out time (2002 SP3)

    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

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Counting out time (2002 SP3)

    Use

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

    orig.Select

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •