+ Reply to Thread
Results 1 to 7 of 7

Efficient Program to loop thru interval range and perform operations - multiple times

  1. #1
    Registered User
    Join Date
    07-06-2012
    Location
    Manassas, VA USA
    MS-Off Ver
    Excel 2003
    Posts
    3

    Efficient Program to loop thru interval range and perform operations - multiple times

    Hi

    I am a fairly experienced Excel user but have no experience with VBA but fear that VBA may be the only way I can do what I want to.

    I want to be able to cycle/loop thru an interval defined by two cell references and add the sum of each cycle step

    For a simple example

    Cell A1: 3
    Cell B1: 10
    Cell C1: sum(5*[Each integer from Cell A1 to B1)

    In other words, I want Cell C1 to generate: (5*3) + (5*4) + (5*5) +....+ (5*10)

    The range of potential values would be the integers from 1 to 5000

    I have read thru some literature (on this great site and others) that suggest various loops in VBA. I am leery of doing this because I need to repeat this formula about 100 times within my spreadsheet and I dont want to get the program bogged down.

    Is there an existing formula that I am missing which would do this? Or is a program via VBA the only way to do it?

    Many thanks in advance for help and advice


    David

  2. #2
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Efficient Program to loop thru interval range and perform operations - multiple times

    Add function to a standard module.

    Please Login or Register  to view this content.
    Then to cell C1: =fivetimes(A1, B1)

  3. #3
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Efficient Program to loop thru interval range and perform operations - multiple times

    dmarzo,

    Welcome to the forum!
    Here's a formula solution:
    Please Login or Register  to view this content.
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  4. #4
    Forum Expert Pepe Le Mokko's Avatar
    Join Date
    05-14-2009
    Location
    Belgium
    MS-Off Ver
    O365 v 2402
    Posts
    13,598

    Re: Efficient Program to loop thru interval range and perform operations - multiple times

    Quote Originally Posted by dmarzo View Post
    Hi


    Cell A1: 3
    Cell B1: 10
    Cell C1: sum(5*[Each integer from Cell A1 to B1)

    In other words, I want Cell C1 to generate: (5*3) + (5*4) + (5*5) +....+ (5*10)


    David
    You formula then boils down to =5*(3+4+5+...)

    The sum of each integer between two values is the sum of an arithmetic progression found by Carl Frederic Gauss

    So in our case the result you need is given by
    Please Login or Register  to view this content.
    Last edited by Pepe Le Mokko; 07-06-2012 at 12:42 PM.

  5. #5
    Registered User
    Join Date
    07-06-2012
    Location
    Manassas, VA USA
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: Efficient Program to loop thru interval range and perform operations - multiple times

    Thank you all so much for the quick replies. Very much appreciated. There is one wrinkle that is throwing me off--I used the 5* for simplicity of example but my actual equation is a little more complicated which I think would mess up Pepe's thought but should still work with Steven or Tigers

    Steven - I am using X (my interval range) in an equation y=Tx^b and I have multiple Ts and bs
    For each loop, I want to take the max value of Tx^b and add it to the loop. I took Steven's suggestion and wrote the following but the script does not recognize "Max". I apologize that I do not know VBA at all

    Tiger - I am going to see if I can get yours to work now

    Function Dinger(rgStart As Range, rgEnd As Range, T11, exp1, T12, exp2, T13, exp3) As Long
    Dim i As Long
    For i = rgStart To rgEnd
    Dinger = Dinger + Max(T11 * i ^ exp1, T12 * i ^ exp2, T13 * 1 ^ exp3)
    Next i
    End Function

  6. #6
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Efficient Program to loop thru interval range and perform operations - multiple times

    Maybe:

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    07-06-2012
    Location
    Manassas, VA USA
    MS-Off Ver
    Excel 2003
    Posts
    3

    Cool Re: Efficient Program to loop thru interval range and perform operations - multiple times

    This made my day - thanks everyone

    This is how I ended up doing it
    I had to switch i to "double" because it can take on decimal values; and I invoked application.max which seems to do the trick of what I needed in terms of taking the max value.
    Again, thanks - I am a VBA newby but excited at how I can expand its use for what I am doing

    Formula: copy to clipboard
    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1