Automating Solver Objective Selection

JaCharger

New Member
Joined
Oct 7, 2022
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hi,

I'm working on a script to automate using Solver. One of the scripts I'm working on is this:

VBA Code:
Private Sub SolverSolver()
Dim ws1 As Worksheet
Dim lRow As Long
Dim cell As Range

Set ws1 = Worksheets("Sheet1")
ws1.Activate
lRow = ws1.Cells(Rows.Count, 1).End(xlUp).Row

    ws1.Range("H2:H4").ClearContents
    
    For Each cell In ws1.Range("H2:H" & Range("H2").End(xlDown).Row)
        If cell.HasFormula = True Then
            SolverOK setCell:=cell, _
                MaxMinVal:=3, _
                ValueOf:="70", _
                byChange:=Range("C2:C" & Range("C2").End(xlDown).Row)
            SolverSolve (True)
            End If
    Next cell
    
    
End Sub

This only makes the first cell that contains a formula into a reference of itself. For example, H21 being the first cell to have a formula in a sample spreadsheet (using =SUMIF) become $H$21. No other cell in the sheet is changed.

The purpose is to detect any cell in the H column that has a formula, set that as the objective in Solver, set the objective value to the value of 70, by changing the entire C column. The =SUMIF formula that is used dictates what gets added.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Bump because I've changed the code slightly...

VBA Code:
Private Sub SolverSolver()

Dim ws1 As Worksheet
Dim lRow As Long
Dim cell As Range

Set ws1 = Worksheets("Sheet1")
ws1.Activate
lRow = ws1.Cells(Rows.Count, 1).End(xlUp).Row
    'clears h2 to h4 because salarytotals puts the total for the first employee number there 3 times...idk why lol
    ws1.Range("H2:H4").ClearContents

    For Each cell In ws1.Range(Range("H2"), Range("H" & lRow))
        If cell.HasFormula = True Then
            SolverOK setCell:=cell, _
                MaxMinVal:=3, _
                ValueOf:=70, _
                Engine:=1, _
                byChange:=Range("C2:C" & Range("C2").End(xlDown).Row)
            SolverSolve UserFinish:=False
            End If
    Next cell
    
End Sub

I'm still running into the issue where the cell is turned into a reference of itself. Of course, replacing the setCell option with a reference to the cell in SolverOK makes the script run fine, but that defeats the purpose of this.
 
Upvote 0
The answer to this would be changing
VBA Code:
SolverOK setCell:=cell
to
VBA Code:
SolverOK setCell:=cell.Address
.
 
Upvote 0
Solution

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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