+ Reply to Thread
Results 1 to 12 of 12

Using Solver from VBA Excel 2010

Hybrid View

  1. #1
    Registered User
    Join Date
    10-03-2011
    Location
    Hong Kong
    MS-Off Ver
    Excel 2003, Excel 2010
    Posts
    26

    Using Solver from VBA Excel 2010

    I have to maintain both an Excel 2003 and an Excel 2010 version of the same code, but the part that calls the solver seems to be not working in 2010 version. In the 2003 version I have:
    ' Fill price formula temporarily
    Worksheets("InstrumentsList").Range("A1").Offset(rowNum - 1, 43) = "= funcPrice(AS" & (rowNum) & ")"
    ' Solver
    SolverReset
    SolverOk SetCell:="AR" & rowNum, MaxMinVal:=3, ValueOf:=mktPrice, ByChange:="AS" & rowNum
    Dim solverResults As Integer
    solverResults = SolverSolve(UserFinish:=True, ShowRef:="SolverIteration")
    where funcPrice() is a function that takes one argument. In Excel 2003 this works perfectly. However in 2010 the solver fails, and as I can see on the status bar close to the bottom of the screen the objective cell is always zero, hence the failure.

    Has anyone encountered similar issue? Is the syntax for calling the solver different in Excel 2010? Thanks!

  2. #2
    Valued Forum Contributor tlafferty's Avatar
    Join Date
    04-08-2011
    Location
    United States, Tacoma, WA
    MS-Off Ver
    Excel 2010, Excel 2013 Customer Preview
    Posts
    1,112

    Re: Using Solver from VBA Excel 2010

    Not sure about the syntax, but if the solver add-in is not loaded, it will fail regardless of syntax...
    If your question has been satisfactorily addressed, please consider marking it solved. Click the Thread Tools dropdown and select Mark thread as solved.
    Also, you might want to add to the user's reputation by clicking the star icon in the lower left corner of the post with the answer- it's why we do what we do...

    Thomas Lafferty
    Analyst/Programmer

  3. #3
    Registered User
    Join Date
    10-03-2011
    Location
    Hong Kong
    MS-Off Ver
    Excel 2003, Excel 2010
    Posts
    26

    Re: Using Solver from VBA Excel 2010

    Do realize that but that is not the reason, I made sure that Solver is added both on the workbook and on the VBA reference...

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Using Solver from VBA Excel 2010

    Does it work if you run Solver from the user interface?
    Entia non sunt multiplicanda sine necessitate

  5. #5
    Registered User
    Join Date
    10-03-2011
    Location
    Hong Kong
    MS-Off Ver
    Excel 2003, Excel 2010
    Posts
    26

    Re: Using Solver from VBA Excel 2010

    I tried setting up a problem (though different from the VBA computation I'm trying to do) and it does work

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Using Solver from VBA Excel 2010

    There's a lot of code missing from what you posted, and your code assumes that InstrumentsList is the active sheet.

    This worked for me.

    Sub x()
        Dim iResult     As Integer
        Dim iRow        As Long
        Dim mktprice    As Double
    
        mktprice = 10
        iRow = 1
    
        With Worksheets("InstrumentsList").Cells(iRow, "AR")
            .FormulaR1C1 = "= funcPrice(rc[-1])"
            SolverReset
            SolverOk SetCell:=.Address, _
                     MaxMinVal:=3, _
                     ValueOf:=mktprice, _
                     ByChange:=.Offset(, -1).Address
        End With
        iResult = SolverSolve(UserFinish:=True, ShowRef:="SolverIteration")
    End Sub
    
    Function funcPrice(d As Double)
        funcPrice = d - 1
    End Function

  7. #7
    Registered User
    Join Date
    10-03-2011
    Location
    Hong Kong
    MS-Off Ver
    Excel 2003, Excel 2010
    Posts
    26

    Re: Using Solver from VBA Excel 2010

    Thanks shg. The same problem persists though, i.e. status bar telling me that the objective is always zero...will try again.

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Using Solver from VBA Excel 2010

    Maybe the problem is in funcPrice.
    Last edited by shg; 06-05-2012 at 06:43 PM.

  9. #9
    Registered User
    Join Date
    10-03-2011
    Location
    Hong Kong
    MS-Off Ver
    Excel 2003, Excel 2010
    Posts
    26

    Re: Using Solver from VBA Excel 2010

    Mmm.....unlikely too, I call and display the function output after using the solver and those are verified. Plus, it has had no problem in the 2003 version. Very weird.

  10. #10
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Using Solver from VBA Excel 2010

    No problem for me in 2010.

  11. #11
    Registered User
    Join Date
    10-03-2011
    Location
    Hong Kong
    MS-Off Ver
    Excel 2003, Excel 2010
    Posts
    26

    Re: Using Solver from VBA Excel 2010

    I placed a break point within funcPrice. Seems to me that, while the "byChange" cell has to be negative to produce the desired value, solver is only willing to plug positive values.

  12. #12
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Using Solver from VBA Excel 2010

    Solver is smarter than that, but the information you've provided is insufficient to even make a guess.

    Good luck.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1