macro solver based on selected row

bluescreamsblack

New Member
Joined
Mar 7, 2018
Messages
3
I have a specific solver that I need run, and all the information is organized per row. However, my data set is large and I don't need solver run for all rows. What I would like is to input a value into a cell (BB4), then have the macro search Column E for matching value. Once the row with the matching value is found, run a solver based on information in that row. All rows are set up the same, so column references will be the same regardless of which row is chosen.

Input number into cell $BB$4.
Search column E for cell value matching $BB$4
Set variables for solver based on row

rVa1l = cell (current row, 95)
rVal2 = cell(current row, 50)
rVal3 = range(current row, 25:26)

Run solver with variables from above
SolverOk SetCell:="rVal1", MaxMinVal:=3, ValueOf:=0, ByChange:="rVal3" _
, Engine:=1, EngineDesc:="GRG Nonlinear"
SolverAdd CellRef:="rVal1", Relation:=1, FormulaText:="rVal1"
SolverAdd CellRef:="rVal2", Relation:=2, FormulaText:="0"
SolverSolve userFinish:=True

I am not sure if it is possible to run solver with variables, rather than cell references.
Thanks,
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
For anyone who stumbles across this thread, I was able to find a solution. Thanks.

Code:
Sub Stress_Solver()
  Dim rFind         As Range
  Dim sAdr1         As String
  Dim sAdr2         As String
  Dim sAdr3         As String
  Dim sAdr4         As String
  Dim sAdr5         As String
Dim intMyVal As String
Dim lngLastRow As Long
Dim strRowNoList As String
Worksheets("Service Stresses").Activate
intMyVal = Cells(4, "BB").Value
Set rFind = Range("E:E").Find(What:=Range("BB4").Value2, LookIn:=xlValues, LookAt:=xlWhole)
lngLastRow = Cells(Rows.Count, "E").End(xlUp).Row 'Search Column A, change as required.
For Each cell In Range("E13:E" & lngLastRow) 'Starting cell is E13, change as required.
    If cell.Value = intMyVal Then
    
            With Rows(rFind.Row)
                
                sAdr1 = .Range("CQ1").Address
                sAdr2 = .Range("AX1:AY1").Address
                sAdr3 = .Range("AX1").Address
                sAdr4 = .Range("CR1").Address
                sAdr5 = .Range("AZ1").Address
            End With
        
           SolverReset
            SolverOk SetCell:=sAdr1, MaxMinVal:=3, ValueOf:=0, ByChange:=sAdr2 _
            , Engine:=1, EngineDesc:="GRG Nonlinear"
            SolverAdd CellRef:=sAdr3, Relation:=1, FormulaText:=sAdr5
            SolverAdd CellRef:=sAdr4, Relation:=2, FormulaText:="0"
            SolverSolve userFinish:=True
        
            strRowNoList = strRowNoList & ", " & cell.Row
            Set rFind = Range("E:E").FindNext(rFind)
        Range("A1").Value = sAdr1
    End If
Next cell
MsgBox "Rows" & strRowNoList & " were solved"
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,732
Members
448,987
Latest member
marion_davis

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