Consulting

Results 1 to 9 of 9

Thread: Solved: Looping sum

  1. #1
    VBAX Regular
    Joined
    Apr 2005
    Posts
    86
    Location

    Solved: Looping sum

    Hey Guys,
    The following piece of coding is meant to add up a number of formfields results and put the final result of the sum in another formfield. When i run it at the moment, it just puts the result of the last formfield that it looks at in there.
    Any ideas what i need to do??

    Many Thanks

    Pete

    [VBA]Sub MoneyInSums()
    iNum = 17
    Do While ActiveDocument.Bookmarks.Exists("MIn" & iNum)
    ActiveDocument.FormFields("TotalMoneyIn").Result = _
    ActiveDocument.FormFields("TotalMoneyIn").Result + ActiveDocument.FormFields("Min" & iNum).Result
    iNum = iNum + 5
    Loop
    End Sub[/VBA]
    If you can't convince them, confuse them!

  2. #2
    VBAX Expert xCav8r's Avatar
    Joined
    May 2005
    Location
    Minneapolis, MN, USA
    Posts
    912
    Location
    I only see it looking in "Min". Maybe you should do a for each?

  3. #3
    VBAX Regular
    Joined
    Apr 2005
    Posts
    86
    Location
    Can you make an example around the basis of my code please??
    If you can't convince them, confuse them!

  4. #4
    VBAX Expert xCav8r's Avatar
    Joined
    May 2005
    Location
    Minneapolis, MN, USA
    Posts
    912
    Location
    This sums the values in every field except TotalMoneyIn.

    [VBA] Dim sngAddItUp as Single
    Dim MyFormField as FormField
    For Each MyFormField in ActiveDocument.FormFields
    If MyFormField.Name <> "TotalMoneyIn" then
    sngAddItUp = sngAddItUp + MyFormField.Result
    End If
    Next MyFormField
    ActiveDocument.FormFields("TotalMoneyIn").Result = sngAddItUp [/VBA]

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Pete,

    You can set the properies of the Formfield to make the calculation.

    So my question to you is do the users fill in the numbers by hand or are you filling it in by code?

    If by hand then I would just set the field options to calculate on exit and make one formfield a formula field for the total!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  6. #6
    VBAX Regular
    Joined
    Apr 2005
    Posts
    86
    Location
    The users fill in the formfields by hand but..... the form never has the same number of formfields on it that add together to make the total. I need an "if exists then add to the total" loop. There could b anything from 1 to unknown number of formfields that to need to be added to the total
    If you can't convince them, confuse them!

  7. #7
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    I don't get this!

    Marco's code loops through all formfields in the document no need to check if it exists...It's in the collection!

    So tell me why is his sub not working for you?
    Perhaps a example would help of the problem.
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  8. #8
    VBAX Regular
    Joined
    Apr 2005
    Posts
    86
    Location
    I mixed what i already had with what Marco's coding to get


    [VBA] Sub MoneyInSums()
    Dim SubTotal As Single
    Dim iNum As Integer
    iNum = 17
    SubTotal = 0
    Do While ActiveDocument.Bookmarks.Exists("MIn" & iNum)
    SubTotal = SubTotal + ActiveDocument.FormFields("Min" & iNum).Result
    iNum = iNum + 5
    Loop
    ActiveDocument.FormFields("TotalMoneyIn").Result = SubTotal
    End Sub [/VBA]
    That sorted it for me, cheers guys

    Pete
    If you can't convince them, confuse them!

  9. #9
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Hi Pete,

    Well the only change I see is the dimension of a Single variable...but who cares....the problem is solved and that's what it's all about!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

Posting Permissions

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