Consulting

Results 1 to 5 of 5

Thread: How to return the sum of variables which change in a loop

  1. #1

    How to return the sum of variables which change in a loop

    Hi there !
    Just wanted to ask you a quick question. I've a VBA code which changes accordingt an input parameter i which changes every time depending where it is in the loop.
    say for example the return value is return(j) which changes for each value of j.

    I want a code which will return the value of the sum of all the return(j) for j=1 to n
    I have tried using
    [VBA]
    Sub g()
    for j =1 to n
    Return (j) = .......someinputformla which is dependent on (j)
    g=worksheetfunction.sum(return(j))
    Cells(1,1) = g
    Next j
    End sub
    [/VBA]
    But it does not work....basically,i just wanna know how to sum the return (j)
    Thanks for yourtime
    Regards
    Haarish
    Last edited by XLGibbs; 02-24-2006 at 05:08 PM. Reason: Added VBA tags

  2. #2
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Haarish

    It isn't clear, to me anyway, what you actually want to sum.

  3. #3
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Hi Haarish,
    Since this appears to be an excel question(reference to worksheet) I am going to move this thread to the Excel help forum so that others looking to help with excel questions can find it.....
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Haarish,
    Welcome to VBAX

    Try something like

    [VBA]
    Sub AddJ()
    Dim g As Double, j As Long
    g = 0
    For j = 1 To [A1]
    g = g + DoStuff(j)
    Next j
    Cells(2, 1) = g
    End Sub

    Function DoStuff(j As Long) as Double
    Select Case j
    Case Is < 3
    DoStuff = j ^ 3
    Case Is < 10
    DoStuff = j ^ 4
    Case Is < 20
    DoStuff = j ^ 2
    End Select
    End Function
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Master XLGibbs's Avatar
    Joined
    Jan 2006
    Location
    state of confusion, but vacation in denial
    Posts
    1,315
    Location
    MD,

    I like the function name! I use the stuff suffix frequently...especially with my ToggleStuff routine I usually use to handle events and screenupdating.

    Hopefully the OP comes back with more questions or to advise if the suggestion worked or could be applied to his need...
    If you have posted the same question at multiple forums, please read this IMPORTANT INFO.

    Please use the thread tools to mark your thread Solved


    Please review the Knowledge Base
    for samples and solutions , or to submit your own!




Posting Permissions

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