Macro to run Solver on multiple columns

Mwalje

New Member
Joined
Sep 16, 2016
Messages
4
I have looked through a number of other related questions, but I haven't been able to really figure out how to create the macro I need. I am very new to VBA, basically ran into an issue that I can't solve without a macro and need to learn it now. Unfortunately I am on a deadline to try and get this done and will have to spend more time learning later.
I am using Solver to find an X value using two equations stored in columns AM and AN. I am using a dummy X and Y values in columns AO and AP, which are replaced with a correct value when I run the Solver Add-In.
The equations are:
<code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; white-space: inherit;">=IF([@[Adjusted Factor % (Uses an Average Turn per Month to Calculate)]]=$BH$3, ([@[Current Stock - MUUS]]/[@Y])-$BI$3,
IF([@[Adjusted Factor % (Uses an Average Turn per Month to Calculate)]]=$BH$4, ([@[Current Stock - MUUS]]/[@Y])-$BI$4,
IF([@[Adjusted Factor % (Uses an Average Turn per Month to Calculate)]]=$BH$5, ([@[Current Stock - MUUS]]/[@Y])-$BI$5,
IF([@[Adjusted Factor % (Uses an Average Turn per Month to Calculate)]]=$BH$6, ([@[Current Stock - MUUS]]/[@Y])-$BI$6,
IF([@[Adjusted Factor % (Uses an Average Turn per Month to Calculate)]]=$BH$7, ([@[Current Stock - MUUS]]/[@Y])-$BI$7)))))</code>And:
<code style="margin: 0px; padding: 1px 5px; border: 0px; font-size: 13px; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; white-space: pre-wrap; background-color: rgb(239, 240, 241);">=(([@[(X)]]/9.5)*12)-[@Y]</code>
I recorded the macro, and need to use a loop to run the Solver for the 8006 rows of the table. The table name is "Table13" in case that helps. The macro, as recorded is:
<code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; white-space: inherit;">' Run_Solver Macro
' Run solver for the array.
'
' Keyboard Shortcut: Ctrl+Shift+S
'

SolverOk SetCell
:="$AN$3", MaxMinVal:=3, ValueOf:=0, ByChange:= _
"$AO$3:$AP$3", Engine:=1, EngineDesc:="GRG Nonlinear"
SolverDelete CellRef
:="$AM$3", Relation:=2, FormulaText:="0"
SolverAdd CellRef
:="$AM$3", Relation:=2, FormulaText:="0"
SolverOk SetCell
:="$AN$3", MaxMinVal:=3, ValueOf:=0, ByChange:= _
"$AO$3:$AP$3", Engine:=1, EngineDesc:="GRG Nonlinear"
SolverOk SetCell
:="$AN$3", MaxMinVal:=3, ValueOf:=0, ByChange:= _
"$AO$3:$AP$3", Engine:=1, EngineDesc:="GRG Nonlinear"
SolverSolve
SolverOk SetCell
:="$AN$4", MaxMinVal:=3, ValueOf:=0, ByChange:= _
"$AO$4:$AP$4", Engine:=1, EngineDesc:="GRG Nonlinear"
End Sub</code>Any and all help would be very much appreciated!
MW
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Welcome to the board.

Perhaps this:

Code:
Sub Mwalje()
  Dim wks           As Worksheet
  Dim rRow          As Range

  Set wks = ActiveSheet

  For Each rRow In wks.ListObjects("Table13").DataBodyRange.EntireRow.Rows
    With rRow
      SolverReset
      SolverOk SetCell:=.Range("AN1").Address, MaxMinVal:=3, ValueOf:=0, ByChange:=.Range("AO1:AP1").Address, Engine:=1
      SolverAdd CellRef:=.Range("AM1").Address, Relation:=2, FormulaText:="0"
      SolverSolve UserFinish:=True
    End With
  Next rRow
End Sub
 
Last edited:
Upvote 0
1. On what line?

2. Have you set a reference to Solver?
 
Upvote 0
The first line. What does that mean? Referencing Solver? Sorry I am absolutely a greenhorn with Macros
 
Upvote 0
The first line?

Code:
Sub Mwalje()

???

What version of Excel are you using?
 
Last edited:
Upvote 0
2013. I think the Solver Add-In was not turned on in the VBA Development Environment. Will retry it this morning.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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