+ Reply to Thread
Results 1 to 6 of 6

Macro - Repeat macro a specific number of times

Hybrid View

  1. #1
    Registered User
    Join Date
    02-18-2013
    Location
    Sri Lanka
    MS-Off Ver
    Excel 2010
    Posts
    3

    Post Macro - Repeat macro a specific number of times

    Hi everyone,

    I'm working with an assignment and i have come across following problem.

    I need to run the following solver macro for the objective cell range P25:P78.

    what I'm doing is, I enter a value to the G25 (actually values to the cell range G25:G78). Here G25=260.
    I need to get the root of the equation entered in the objective cell(P25) and root will be displayed in the J25.
    The following code is generated for P25. I need to get a code to repeat this process in the cell range P25:P78.

    Sub Solve_n_repeat()
    '
    ' Solve_n_repeat Macro
    ' Solve for R1 and repeat
    '
    ' Keyboard Shortcut: Ctrl+r
    '
        Range("G25").Select
        ActiveCell.FormulaR1C1 = "260"
        Range("P25").Select
        SolverOk SetCell:="$P$25", MaxMinVal:=3, ValueOf:=0, ByChange:="$J$25", Engine _
            :=1, EngineDesc:="GRG Nonlinear"
        SolverOk SetCell:="$P$25", MaxMinVal:=3, ValueOf:=0, ByChange:="$J$25", Engine _
            :=1, EngineDesc:="GRG Nonlinear"
        SolverSolve
    End Sub
    Please help me.
    Thanx in advance!!

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Macro - Repeat macro a specific number of times

    Untested - but you can try this -
    Sub Solve_n_repeat()
    Dim i As Long
    ' Solve_n_repeat Macro
    ' Solve for R1 and repeat
    '
    ' Keyboard Shortcut: Ctrl+r
    '
    With Worksheets("Sheet1")
        For i = 25 To 78
            .Range("G" & i).Value = "260"
            SolverOk SetCell:="$P$" & i, MaxMinVal:=3, ValueOf:=0, ByChange:="$J$" & i, Engine _
                :=1, EngineDesc:="GRG Nonlinear"
            SolverOk SetCell:="$P$" & i, MaxMinVal:=3, ValueOf:=0, ByChange:="$J$" & i, Engine _
                :=1, EngineDesc:="GRG Nonlinear"
            SolverSolve
        Next i
    End With
    End Sub
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Registered User
    Join Date
    02-18-2013
    Location
    Sri Lanka
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Macro - Repeat macro a specific number of times

    Hi,
    Thanx for your help. I think I got it correct after some adjustments. here is my new code.

    Sub Solve()
    Dim i As Long
    ' Solve Macro
    ' use solver function
    '
    'Keyboard Shortcut: Ctrl+r
    
    With Worksheets("Flame cal")
        For i = 25 To 78
            SolverOk SetCell:="$P$" & i, MaxMinVal:=3, ValueOf:=0, ByChange:="$J$" & i, Engine _
                :=1, EngineDesc:="GRG Nonlinear"
            SolverOk SetCell:="$P$" & i, MaxMinVal:=3, ValueOf:=0, ByChange:="$J$" & i, Engine _
                :=1, EngineDesc:="GRG Nonlinear"
            SolverSolve
        Next i
    End With
    End Sub
    In this way I'm getting my solution but for each and every cell($P$) the solver results pop-up window appears and I have to click the "OK" for 54 times. Is there any way to avoid that.

    In my solution I have removed
    .Range("G" & i).Value = "260"
    part because I'm entering different values(not 260 for all the cells).
    Is there any method to write a code to get "the value I'm entering" instead of "260".

    Thanx for the help!!

  4. #4
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,787

    Re: Macro - Repeat macro a specific number of times

    To get rid of the pop-up window use

    SolverSolve UserFinish:= True
    I would also recomend adding the line

    SolverReset
    to avoid buildup of the same constraint.

    Alf
    Last edited by Alf; 02-25-2013 at 03:18 AM. Reason: seems I can't spell

  5. #5
    Registered User
    Join Date
    02-18-2013
    Location
    Sri Lanka
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Macro - Repeat macro a specific number of times

    Hi,
    It's working nicely. this is what I required. thanx for all!!

  6. #6
    Forum Expert Alf's Avatar
    Join Date
    03-13-2004
    Location
    Gothenburg/Mullsjoe, Sweden
    MS-Off Ver
    Excel 2019 and not sure I like it
    Posts
    4,787

    Re: Macro - Repeat macro a specific number of times

    Thanks for feedback and rep!

    Alf

+ 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