why these two macros (about Solver) give different results?

novice_2010

Board Regular
Joined
Mar 18, 2010
Messages
105
Hello, All,

My first macro is:

Sub Worked_Solver_Macro()

Range("A5").FormulaR1C1 = "5"
Range("A6").FormulaR1C1 = "1"
Range("A7").FormulaR1C1 = "=(R[-2]C-R[-1]C)^2"
SolverOk SetCell:="$A$7", MaxMinVal:=2, ValueOf:="0", ByChange:="$A$5"
SolverSolve

End Sub
And the result is:

2ql6gc1.jpg




My second macro is:

Sub Not_Worked_Solver_Macro()
Dim Variable_X as Long

Range("A1").FormulaR1C1 = "5"
Variable_X=Range("A1").value

Range("A5") = Variable_X

Range("A6").FormulaR1C1 = "1"

Range("A7").FormulaR1C1 = "=(R[-2]C-R[-1]C)^2"
SolverOk SetCell:="$A$7", MaxMinVal:=2, ValueOf:="0", ByChange:="$A$1"
SolverSolve

End Sub
And the result is:

1z5t9ft.jpg



I thought the Solver result would be the same. However, they are not.
What mistakes did i make?
 
The code I show here is actually a much more simplified version of my real code (which is hundreds of lines)

I am showing this code because I hope I could get some solutions of how to deal with variables when using solver.
 
Upvote 0

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
In this code,

Code:
Sub x()
    Dim x  As Double
 
    Range("A1").Value = 5
    x = Range("A1").Value
    Range("A5").Value = x
    Range("A6").Value = 1
    Range("A7").FormulaR1C1 = "=(R[-2]C-R[-1]C)^2"
 
    SolverOk SetCell:="A7", MaxMinVal:=2, ByChange:="A1"
    SolverSolve
End Sub

Cell A7 has no dependency on A1 when Solver iterates; A7 depends only on A6 and A5.

This works:

Code:
Sub y()
    Dim x  As Double
 
    Range("A1").Value = 5
    x = Range("A1").Value
    Range("A5").Value = x
    Range("A6").Value = 1
    Range("A7").FormulaR1C1 = "=(R[-2]C-R[-1]C)^2"
 
    SolverOk SetCell:="A7", MaxMinVal:=2, ByChange:="A5"
    SolverSolve
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,581
Messages
6,179,668
Members
452,936
Latest member
anamikabhargaw

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