HELP! Solver VBA variable range loop

jimmyvba

New Member
Joined
Jul 19, 2013
Messages
20
Hi Guys,

Would be really greatful for a solution. I am trying to write a VBA loop for solver function where the Target and constraints will depend on the futures contract chosen in a cell but am just not being able to make it work. Here is the macro, that i came up with. Here, i have kept the futures contract fixed as ERM4 but in reality it would be linked to a cell. Also, i am aware the solver works only for active sheet if i am not wrong. I am fine even if it works on the same sheet as the target and constraints but ideally i would like it to work on a different sheet. Here is the code. Any help would be greatly appreciated. Thanks in advance.

Sub Solv()
Dim sigma_rng As Range
Dim alpha_rng As Range
Dim beta_rng As Range
Dim rho_rng As Range
Dim target_rng As Range
Dim rng As Range
Dim i As Integer
Dim i_row As Integer
Dim sheet_name As String
futures_name = "ERM4"
i = 9
Do While Worksheets("Euribor").Cells(i, 1).Value > 0
If Worksheets("Euribor").Cells(i, 4).Value = futures_name Then
i_row = i
End If
i = i + 1
Loop

Set sigma_rng = Worksheets("Euribor").Cell(i_row, 19)

Set alpha_rng = Worksheets("Euribor").Cells(i_row, 20)
Set beta_rng = Worksheets("Euribor").Cells(i_row, 21)
Set rho_rng = Worksheets("Euribor").Cells(i_row, 22)
Set target_rng = Worksheets("Euribor").Cells(i_row, 24)
Set rng = Range(Worksheets("Euribor").Cells(i_row, 19), Worksheets("Euribor").Cells(i_row, 22))

'SolverReset
SolverOk SetCell:=target_rng, MaxMinVal:=2, ByChange:=rng, _
Engine:=1, EngineDesc:="GRG Nonlinear"
SolverAdd CellRef:=alpha_rng, Relation:=3, FormulaText:="0"
SolverAdd CellRef:=beta_rng, Relation:=3, FormulaText:="0"
SolverAdd CellRef:=beta_rng, Relation:=1, FormulaText:="1"
SolverAdd CellRef:=rho_rng, Relation:=3, FormulaText:="-0.9999"
SolverAdd CellRef:=rho_rng, Relation:=1, FormulaText:="0.9999"
SolverSolve UserFinish:=False

End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.

Forum statistics

Threads
1,215,723
Messages
6,126,470
Members
449,315
Latest member
misterzim

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