Looping the Solver function using Macros

abhishekgupta

New Member
Joined
May 12, 2022
Messages
4
Office Version
  1. 365
  2. 2021
  3. 2019
Platform
  1. Windows
Sub Macro1()
'
' Macro1 Macro
'

'
SolverOk SetCell:="$BD$8", MaxMinVal:=3, ValueOf:=0, ByChange:="$AJ$8", Engine _
:=1, EngineDesc:="GRG Nonlinear"""
SolverOk SetCell:="$BD$8", MaxMinVal:=3, ValueOf:=0, ByChange:="$AJ$8", Engine _
:=1, EngineDesc:="GRG Nonlinear"""
SolverSolve
End Sub

I want to run the above code for cell B8 till B15 (& AJ8 till AJ15 correspondingly), how to run this? Please suggest the appropriate code changes.
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Welcome to the Board!

I don't really use Solver much, but it seems that your question is more about loops and ranges than it is Solver.
See if this works for you:
VBA Code:
Sub Macro1()
'
' Macro1 Macro

    Dim r As Long
    Dim rng1 As Range
    Dim rng2 As Range
    
'   Loop through rows 8 to 15
    For r = 8 To 15
'       Build range references
        Set rng1 = Range("BD" & r)
        Set rng2 = Range("AJ" & r)
'       Run solver
        SolverOk SetCell:=rng1, MaxMinVal:=3, ValueOf:=0, ByChange:=rng2, Engine _
            :=1, EngineDesc:="GRG Nonlinear"""
        SolverOk SetCell:=rng1, MaxMinVal:=3, ValueOf:=0, ByChange:=rng2, Engine _
            :=1, EngineDesc:="GRG Nonlinear"""
        SolverSolve
    Next r
    
End Sub
 
Upvote 0
Thanks for the reply, but did not work... after running the macros, the AJ & BD row started showing the cell names...
image excel.jpg
 
Upvote 0
Since you did not include column headers in your image, I have no idea which of those columns and AJ and BD.

And was the original code that you posted actually working for row 8 or not?
I was working off of the assumption that it was.
 
Upvote 0
I have done this successfully, but I did it a little differently. I put each call to Solver in a separate macro rather than a loop. However, I don't see why a loop wouldn't work. There are some Solver commands that I don't see in your code though.
One is SolverAdd which are your constraints.
Also, my SolverSolve command is SolverSolve userfinish:=True
The last command is Solverreset

I did get my code by recording a macro which as you probably know sometimes generates extra code that may not be necessary. Nevertheless, that's what I did and it works. Hope this is helpful.
 
Upvote 0
Thanks for the reply, but did not work... after running the macros, the AJ & BD row started showing the cell names...
I guess I don't understand quite what the issue is. I don't see anything odd in your last image.
In looking at the last image you posted, can you explain to us exactly what the current issue is?
 
Upvote 0

Forum statistics

Threads
1,214,942
Messages
6,122,367
Members
449,080
Latest member
Armadillos

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top