Using a simple solver idea using VBA - ByChange cells don't change?

MartinL

Well-known Member
Joined
Oct 16, 2008
Messages
1,141
Office Version
  1. 365
Platform
  1. Windows
I am having trouble getting my sheet to shop the results after running my Macro.

I am using 1 of 3 CommandButtons to run 1 of 3 Macros - Scenarios (and there has to be a better way of this)

example

C9:C12 have the values to adjust
C13 is the Objective Value
H15 is the value to Match

(Button 2 = C14, H16)
(Button 3 = C15, H17)

My 3 buttons are very similar but here is Macro 1

Code:
Private Sub CommandButton1_Click()
'
' Macro1 Macro
' Calculate widgets required to match Subscription Gross Margin
'
'
SolverReset


    ' Set up your solver here...
    SolverOk SetCell:="$C$13", MaxMinVal:=3, ValueOf:="$G$15", ByChange:="$C$9:$C$12", _
        Engine:=1, EngineDesc:="GRG Nonlinear"


    ' Execute solve
    SolverSolve True


End Sub

The problem is if I run the solver manually it works
If I use the VBA V9:C12 do not change

[Note: SolverSolve True - This eliminates the dialog box]


any ideas
 
Last edited:

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
The code below works for me from a command button. Try running Solver manually once and then click the button.
Another thing to try is to eliminate the button and test the code from a standard module.


Code:
' this goes at the sheet module
Private Sub CommandButton1_Click()
Dim objc$, vof
objc = "$d$6"                       ' address of objective cell
vof = [b16]                         ' cell containing desired value
SolverOk SetCell:=objc, MaxMinVal:=3, ValueOf:=vof, ByChange:="$B$3:$B$5", _
Engine:=2, EngineDesc:="Simplex LP"
SolverSolve
End Sub
 
Upvote 0
The code below works for me from a command button. Try running Solver manually once and then click the button.
Another thing to try is to eliminate the button and test the code from a standard module.

Thanks Worf
Live Long and Prosper
 
Last edited:
Upvote 0
You are welcome.

Place a message box in the code to make sure it is running.
 
Upvote 0

Forum statistics

Threads
1,224,862
Messages
6,181,458
Members
453,042
Latest member
AbdelrahmanExcel

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