+ Reply to Thread
Results 1 to 3 of 3

Error message when using the Solver in a VBA macro loop

Hybrid View

  1. #1
    Mathieu Fournier
    Guest

    Error message when using the Solver in a VBA macro loop

    Hi everybody,

    In Excel 2003 (Windows XP), I am trying to use the solver from a VBA macro
    to solve a formula referring to its own result. The formula is included in a
    spreadsheet table and is to be solved for every row of the table..

    When running the macro, it is interrupted before completion and I get the
    message: "Solver: an error has occurred, or available memory is saturated".

    While trying to resolve the problem, I found out that the simple fact of
    opening the input parameter msgbox of the Solver from the menu
    Tool/Solver... and to exit it without having made any change solved the
    problem!?! Afterwards, the macro could be used any number of time without
    any problem.

    It seems that the simple fact of opening the input parameter msgbox of the
    Solver from the menu
    Tool/Solver... activated some paramèters of the Solver
    which are necessary to run my macro.

    Here is the VBA code for my procedure:

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$B$2" Then
    Dim i As Long 'declare variable i loop
    Dim x As Long 'declare variable x row
    ' A loop that will solve the equation for the 26 rows of my table.
    i = 1
    For i = 1 To 26
    x = i + 6 'set row number
    ' Set up the parameters for the model.
    ' Set the target cell Ex to a maximum value by changing cell
    Fx.
    SolverReset
    ' Add constraint: Ex = Fx.
    SolverAdd CellRef:=Cells(x, 5).Address, Relation:=2, _
    FormulaText:=Cells(x, 6).Address
    SolverOK SetCell:=Cells(x, 5).Address, MaxMinVal:=1, _
    ByChange:=Cells(x, 6).Address
    ' Solve the model but do not display the Solver Results
    dialog box.
    SolverSolve UserFinish:=True
    ' Delete constraint: Ex = Fx.
    SolverDelete CellRef:=Cells(x, 5).Address, Relation:=2, _
    FormulaText:=Cells(x, 6).Address
    ' Finish and keep the final results.
    SolverFinish KeepFinal:=1
    Next i
    End If
    End Sub


    What would I need to change to get this macro to work right? (without having
    to first open the input parameter msgbox of the Solver from the menu
    Tool/Solver... and to exit it).

    I tried using the SolverOptions command to modify a few of the advanced
    options of the solver but it did not help my case.


    Thanks for your help,

    Mathieu Fournier




  2. #2
    Tushar Mehta
    Guest

    Re: Error message when using the Solver in a VBA macro loop

    Somewhere along the way (2000? 2002?), something happened between the=20
    XL VBA and Solver interface. Now, it appears one must force Solver to=20
    'initialize' itself. Stick in a SOLVER.Auto_open before the For loop.

    Also, note that embedding a Solver optimization inside the=20
    Worksheet_Change procedure is a prescription to a guaranteed disaster. =20
    It will result in an infinite recursive loop -- or, if you are unlucky,=20
    in wrong results. How? Think of this: how did the code in the=20
    procedure get started? When you call Solver, what will it do? Change=20
    something in the worksheet, right? What will XL do because of that? =20
    And, you will be...back where?

    The common solution is to set EnableEvents to False. But, I don't know=20
    how Solver operates to confidently say that it would be safe to do so. =20
    That leaves you with a self implemented switch. Something along the=20
    lines of:

    Option Explicit
    Dim AlreadyBusy As Boolean
    Private Sub Worksheet_Change(ByVal Target As Range)
    If AlreadyBusy Then Exit Sub '<<<<<
    AlreadyBusy =3D True
    On Error GoTo ErrXIT
    'do my stuff
    AlreadyBusy =3D False
    Exit Sub
    ErrXIT:
    'clean up the results of the error
    AlreadyBusy =3D False
    End Sub

    But, honestly, you are best off not embedding solver optimizations=20
    inside worksheet change (or calculate or selectionchange or any such)=20
    events.

    --=20
    Regards,

    Tushar Mehta
    www.tushar-mehta.com
    Excel, PowerPoint, and VBA add-ins, tutorials
    Custom MS Office productivity solutions

    In article <uLgJaZTHFHA.1996@TK2MSFTNGP12.phx.gbl>,=20
    mathieu_fournier@hotmail.com says...
    > Hi everybody,
    >=20
    > In Excel 2003 (Windows XP), I am trying to use the solver from a VBA macr=

    o=20
    > to solve a formula referring to its own result. The formula is included i=

    n a=20
    > spreadsheet table and is to be solved for every row of the table..
    >=20
    > When running the macro, it is interrupted before completion and I get the=

    =20
    > message: "Solver: an error has occurred, or available memory is saturated=

    ".
    >=20
    > While trying to resolve the problem, I found out that the simple fact of
    > opening the input parameter msgbox of the Solver from the menu
    > Tool/Solver... and to exit it without having made any change solved the=

    =20
    > problem!?! Afterwards, the macro could be used any number of time without=

    =20
    > any problem.
    >=20
    > It seems that the simple fact of opening the input parameter msgbox of th=

    e=20
    > Solver from the menu
    > Tool/Solver... activated some param=E8ters of the Solver
    > which are necessary to run my macro.
    >=20
    > Here is the VBA code for my procedure:
    >=20
    > Private Sub Worksheet_Change(ByVal Target As Range)
    > If Target.Address =3D "$B$2" Then
    > Dim i As Long 'declare variable i loop
    > Dim x As Long 'declare variable x row
    > ' A loop that will solve the equation for the 26 rows of my table=

    ..
    > i =3D 1
    > For i =3D 1 To 26
    > x =3D i + 6 'set row number
    > ' Set up the parameters for the model.
    > ' Set the target cell Ex to a maximum value by changing c=

    ell=20
    > Fx.
    > SolverReset
    > ' Add constraint: Ex =3D Fx.
    > SolverAdd CellRef:=3DCells(x, 5).Address, Relation:=3D2, =

    _
    > FormulaText:=3DCells(x, 6).Address
    > SolverOK SetCell:=3DCells(x, 5).Address, MaxMinVal:=3D1, =

    _
    > ByChange:=3DCells(x, 6).Address
    > ' Solve the model but do not display the Solver Results=

    =20
    > dialog box.
    > SolverSolve UserFinish:=3DTrue
    > ' Delete constraint: Ex =3D Fx.
    > SolverDelete CellRef:=3DCells(x, 5).Address, Relation:=3D=

    2, _
    > FormulaText:=3DCells(x, 6).Address
    > ' Finish and keep the final results.
    > SolverFinish KeepFinal:=3D1
    > Next i
    > End If
    > End Sub
    >=20
    >=20
    > What would I need to change to get this macro to work right? (without hav=

    ing=20
    > to first open the input parameter msgbox of the Solver from the menu
    > Tool/Solver... and to exit it).
    >=20
    > I tried using the SolverOptions command to modify a few of the advanced=

    =20
    > options of the solver but it did not help my case.
    >=20
    >=20
    > Thanks for your help,
    >=20
    > Mathieu Fournier
    >=20
    >=20
    >=20
    >=20


  3. #3
    Mathieu Fournier
    Guest

    Re: Error message when using the Solver in a VBA macro loop

    Thanks for your tips, Tushar

    It works just fine now.


    "Tushar Mehta" <tmUnderscore200310@tushar-mehta.SeeOhEm> wrote in message
    news:MPG.1c8ce04cac1cce94989b09@news-server.rochester.rr.com...
    Somewhere along the way (2000? 2002?), something happened between the
    XL VBA and Solver interface. Now, it appears one must force Solver to
    'initialize' itself. Stick in a SOLVER.Auto_open before the For loop.

    Also, note that embedding a Solver optimization inside the
    Worksheet_Change procedure is a prescription to a guaranteed disaster.
    It will result in an infinite recursive loop -- or, if you are unlucky,
    in wrong results. How? Think of this: how did the code in the
    procedure get started? When you call Solver, what will it do? Change
    something in the worksheet, right? What will XL do because of that?
    And, you will be...back where?

    The common solution is to set EnableEvents to False. But, I don't know
    how Solver operates to confidently say that it would be safe to do so.
    That leaves you with a self implemented switch. Something along the
    lines of:

    Option Explicit
    Dim AlreadyBusy As Boolean
    Private Sub Worksheet_Change(ByVal Target As Range)
    If AlreadyBusy Then Exit Sub '<<<<<
    AlreadyBusy = True
    On Error GoTo ErrXIT
    'do my stuff
    AlreadyBusy = False
    Exit Sub
    ErrXIT:
    'clean up the results of the error
    AlreadyBusy = False
    End Sub

    But, honestly, you are best off not embedding solver optimizations
    inside worksheet change (or calculate or selectionchange or any such)
    events.

    --
    Regards,

    Tushar Mehta
    www.tushar-mehta.com
    Excel, PowerPoint, and VBA add-ins, tutorials
    Custom MS Office productivity solutions

    In article <uLgJaZTHFHA.1996@TK2MSFTNGP12.phx.gbl>,
    mathieu_fournier@hotmail.com says...
    > Hi everybody,
    >
    > In Excel 2003 (Windows XP), I am trying to use the solver from a VBA macro
    > to solve a formula referring to its own result. The formula is included in
    > a
    > spreadsheet table and is to be solved for every row of the table..
    >
    > When running the macro, it is interrupted before completion and I get the
    > message: "Solver: an error has occurred, or available memory is
    > saturated".
    >
    > While trying to resolve the problem, I found out that the simple fact of
    > opening the input parameter msgbox of the Solver from the menu
    > Tool/Solver... and to exit it without having made any change solved the
    > problem!?! Afterwards, the macro could be used any number of time without
    > any problem.
    >
    > It seems that the simple fact of opening the input parameter msgbox of the
    > Solver from the menu
    > Tool/Solver... activated some paramèters of the Solver
    > which are necessary to run my macro.
    >
    > Here is the VBA code for my procedure:
    >
    > Private Sub Worksheet_Change(ByVal Target As Range)
    > If Target.Address = "$B$2" Then
    > Dim i As Long 'declare variable i loop
    > Dim x As Long 'declare variable x row
    > ' A loop that will solve the equation for the 26 rows of my table.
    > i = 1
    > For i = 1 To 26
    > x = i + 6 'set row number
    > ' Set up the parameters for the model.
    > ' Set the target cell Ex to a maximum value by changing
    > cell
    > Fx.
    > SolverReset
    > ' Add constraint: Ex = Fx.
    > SolverAdd CellRef:=Cells(x, 5).Address, Relation:=2, _
    > FormulaText:=Cells(x, 6).Address
    > SolverOK SetCell:=Cells(x, 5).Address, MaxMinVal:=1, _
    > ByChange:=Cells(x, 6).Address
    > ' Solve the model but do not display the Solver Results
    > dialog box.
    > SolverSolve UserFinish:=True
    > ' Delete constraint: Ex = Fx.
    > SolverDelete CellRef:=Cells(x, 5).Address, Relation:=2, _
    > FormulaText:=Cells(x, 6).Address
    > ' Finish and keep the final results.
    > SolverFinish KeepFinal:=1
    > Next i
    > End If
    > End Sub
    >
    >
    > What would I need to change to get this macro to work right? (without
    > having
    > to first open the input parameter msgbox of the Solver from the menu
    > Tool/Solver... and to exit it).
    >
    > I tried using the SolverOptions command to modify a few of the advanced
    > options of the solver but it did not help my case.
    >
    >
    > Thanks for your help,
    >
    > Mathieu Fournier
    >
    >
    >
    >




+ 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