+ Reply to Thread
Results 1 to 13 of 13

Looping Excel Formulas

Hybrid View

  1. #1
    Registered User
    Join Date
    04-25-2012
    Location
    Singapore
    MS-Off Ver
    Excel 2010
    Posts
    7

    Looping Excel Formulas

    I had written a rather complicated excel spreadsheet previously.
    Now, I need to loop it.

    I have attached the spreadsheet and it works like this
    1) Input: I enter flow rate between 0.036 to 1.61 into the red box
    2) The spreadsheet does some lengthy calculations
    3) Output: The spreadsheet produces 2 output values in the yellow boxes - frequency and amplitude.

    Now, I need to repeat this procedure many times for different inputs to plot an input vs. output graph.

    I.e. I need to have inputs from 0.036 to 1.61 in steps of 0.001, and get the corresponding output.
    In the attached "To Plot" worksheet is the sample result which I should get.

    If it was C, the loop would go something like this.

    int x = 0.036;
    while (x < 1.61) {

    //(read input, calculate, produce output, save to file)

    x=x+0.001;
    }


    So how can I implement this? Thanks in advance for your replies!
    Attached Files Attached Files
    Last edited by boiledbeans; 04-25-2012 at 10:28 AM.

  2. #2
    Forum Expert DGagnon's Avatar
    Join Date
    02-23-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2003, 2007
    Posts
    1,645

    Re: Looping Excel Formulas

    Try this macro out, it should do what you are asking

    Sub LoopInOut()
        For i = 3 To Sheets("To Plot").Range("A100000").End(xlUp).Row
            Sheets("GUI").Range("C10").Value = Sheets("To Plot").Cells(i, 1).Value
            Application.Calculate
            Sheets("To Plot").Cells(i, 2).Value = Sheets("GUI").Range("C15").Value
            Sheets("To Plot").Cells(i, 3).Value = Sheets("GUI").Range("C14").Value
        Next i
    End Sub
    If you liked my solution, please click on the Star -- to add to my reputation

    If your issue as been resolved, please clearly state so and mark the thread as [SOLVED] using the thread tools just above the first post.

  3. #3
    Registered User
    Join Date
    04-25-2012
    Location
    Singapore
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Looping Excel Formulas

    Quote Originally Posted by DGagnon View Post
    Try this macro out, it should do what you are asking

    Sub LoopInOut()
        For i = 3 To Sheets("To Plot").Range("A100000").End(xlUp).Row
            Sheets("GUI").Range("C10").Value = Sheets("To Plot").Cells(i, 1).Value
            Application.Calculate
            Sheets("To Plot").Cells(i, 2).Value = Sheets("GUI").Range("C15").Value
            Sheets("To Plot").Cells(i, 3).Value = Sheets("GUI").Range("C14").Value
        Next i
    End Sub
    Wow! This works like a charm!

    Is it possible to modify it so I can have 3 input parameters?
    The first flow rate (0.036), final flow rate (1.61) and increment (0.001)?

    This is for future expansion, in case the three parameters need to be changed.
    So that means the macro automatically creates columns A, B, C in "To Plot" instead of just reading A and outputting to B & C.

    Thanks for your help, I really appreciate it!

  4. #4
    Forum Expert ben_hensel's Avatar
    Join Date
    01-26-2012
    Location
    Northeast USA
    MS-Off Ver
    Office 365
    Posts
    2,043

    Re: Looping Excel Formulas

    For me...

    lessee, 1.61 - 0.036 = 1.574; 1.574 = 0.001 = 1574

    So you're looking at about 1600 data points.

    You've got 2010; the max data points was bumped from 256 in 2007 to like 32,000.

    Just make a chart dood.

  5. #5
    Forum Expert DGagnon's Avatar
    Join Date
    02-23-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2003, 2007
    Posts
    1,645

    Re: Looping Excel Formulas

    if you specify an input sheet with values, i can modify to add the output in what ever format you are looking for.

  6. #6
    Registered User
    Join Date
    04-25-2012
    Location
    Singapore
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Looping Excel Formulas

    I have attached a modified file.

    In the "To plot" worksheet are the 3 input values.
    Attached Files Attached Files

  7. #7
    Forum Expert DGagnon's Avatar
    Join Date
    02-23-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2003, 2007
    Posts
    1,645

    Re: Looping Excel Formulas

    Try this:

    Sub LoopInOut()
        i = 4
        With Sheets("To Plot")
            .Range("A" & i - 1).Value = .Range("E5").Value
            Do Until .Range("A" & i - 1).Value >= .Range("F5").Value
               .Range("A" & i).Value = .Range("A" & i - 1).Value + .Range("G5").Value
               i = i + 1
            Loop
        
            For i = 3 To .Range("A100000").End(xlUp).Row
                Sheets("GUI").Range("C10").Value = .Cells(i, 1).Value
                Application.Calculate
                .Cells(i, 2).Value = Sheets("GUI").Range("C15").Value
                .Cells(i, 3).Value = Sheets("GUI").Range("C14").Value
            Next i
        End With
    End Sub

  8. #8
    Registered User
    Join Date
    04-25-2012
    Location
    Singapore
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Looping Excel Formulas

    Quote Originally Posted by DGagnon View Post
    Try this:

    Sub LoopInOut()
        i = 4
        With Sheets("To Plot")
            .Range("A" & i - 1).Value = .Range("E5").Value
            Do Until .Range("A" & i - 1).Value >= .Range("F5").Value
               .Range("A" & i).Value = .Range("A" & i - 1).Value + .Range("G5").Value
               i = i + 1
            Loop
        
            For i = 3 To .Range("A100000").End(xlUp).Row
                Sheets("GUI").Range("C10").Value = .Cells(i, 1).Value
                Application.Calculate
                .Cells(i, 2).Value = Sheets("GUI").Range("C15").Value
                .Cells(i, 3).Value = Sheets("GUI").Range("C14").Value
            Next i
        End With
    End Sub
    Thanks!
    Works great except for one slight problem - it overshoots by one increment, i.e. stopping at 1.611 instead of 1.61

    Edit: The last value, to be accurate is 1.61099999999993, so maybe it's due to some rounding problems?
    The rounding problem seems to start between rows 1018 and 1019, with input values 1.051 and 1.05199999999999
    Last edited by boiledbeans; 04-25-2012 at 12:13 PM.

  9. #9
    Forum Expert DGagnon's Avatar
    Join Date
    02-23-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2003, 2007
    Posts
    1,645

    Re: Looping Excel Formulas

    i added rounding to the 4th digit here:

    Sub LoopInOut()
        i = 4
        With Sheets("To Plot")
            .Range("A" & i - 1).Value = .Range("E5").Value
            Do Until .Range("A" & i - 1).Value >= .Range("F5").Value
               .Range("A" & i).Value = Round(.Range("A" & i - 1).Value + .Range("G5").Value, 4)
               i = i + 1
            Loop
        
            For i = 3 To .Range("A100000").End(xlUp).Row
                Sheets("GUI").Range("C10").Value = .Cells(i, 1).Value
                Application.Calculate
                .Cells(i, 2).Value = Sheets("GUI").Range("C15").Value
                .Cells(i, 3).Value = Sheets("GUI").Range("C14").Value
            Next i
        End With
    End Sub

  10. #10
    Registered User
    Join Date
    04-25-2012
    Location
    Singapore
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Looping Excel Formulas

    Quote Originally Posted by DGagnon View Post
    i added rounding to the 4th digit here:

    Sub LoopInOut()
        i = 4
        With Sheets("To Plot")
            .Range("A" & i - 1).Value = .Range("E5").Value
            Do Until .Range("A" & i - 1).Value >= .Range("F5").Value
               .Range("A" & i).Value = Round(.Range("A" & i - 1).Value + .Range("G5").Value, 4)
               i = i + 1
            Loop
        
            For i = 3 To .Range("A100000").End(xlUp).Row
                Sheets("GUI").Range("C10").Value = .Cells(i, 1).Value
                Application.Calculate
                .Cells(i, 2).Value = Sheets("GUI").Range("C15").Value
                .Cells(i, 3).Value = Sheets("GUI").Range("C14").Value
            Next i
        End With
    End Sub
    Thanks! It works now!
    Just one more request, I tried to modify the code so it produces one row in each go, instead or generating column A first, then using the column A data to generate column B and C.

    But the problem is the last row's column B and C are blank.
    i.e.
    A B C
    1.61 (blank) (blank)

    Maybe I've done something wrongly in the code?

    Sub LoopInOut()
        i = 4
        With Sheets("To Plot")
            .Range("A" & i - 1).Value = .Range("E5").Value
            Do Until .Range("A" & i - 1).Value >= .Range("F5").Value
               .Range("A" & i).Value = Round(.Range("A" & i - 1).Value + .Range("G5").Value, 4)
               
                Sheets("GUI").Range("C10").Value = .Cells(i - 1, 1).Value
                Application.Calculate
                .Cells(i - 1, 2).Value = Sheets("GUI").Range("C15").Value
                .Cells(i - 1, 3).Value = Sheets("GUI").Range("C14").Value
               i = i + 1
            Loop
        
        End With
    End Sub

  11. #11
    Forum Expert DGagnon's Avatar
    Join Date
    02-23-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2003, 2007
    Posts
    1,645

    Re: Looping Excel Formulas

    this should work for you

    Sub LoopInOut()
        i = 3
        With Sheets("To Plot")
            .Range("A" & i).Value = .Range("E5").Value
            Do Until .Range("A" & i - 1).Value >= .Range("F5").Value And i > 3
                If i > 3 Then .Range("A" & i).Value = Round(.Range("A" & i - 1).Value + .Range("G5").Value, 4)
                Sheets("GUI").Range("C10").Value = .Cells(i, 1).Value
                Application.Calculate
                .Cells(i, 2).Value = Sheets("GUI").Range("C15").Value
                .Cells(i, 3).Value = Sheets("GUI").Range("C14").Value
                i = i + 1
            Loop
        End With
    End Sub

  12. #12
    Registered User
    Join Date
    04-25-2012
    Location
    Singapore
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Looping Excel Formulas

    Perfect, thanks!

    Added a star!

  13. #13
    Forum Expert DGagnon's Avatar
    Join Date
    02-23-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2003, 2007
    Posts
    1,645

    Re: Looping Excel Formulas

    Thank you, Glad I could help

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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