Condividi tramite

VBA - gestione errori Solver

Anonimo
2017-05-08T15:04:47+00:00

Ciao a tutti

ho creato una piccola macro che esegue calcoli con il solver.

Il codice funziona perfettamente.

Quello che non sono riuscito a fare è eseguire una seconda macro che in caso di errore mi segnali che tipo di errore c'è stato.

Da quel che ho letto, dovrebbe farsi così:

Mia Sub

....

....

answer = SolverSolve(False, "SolvErr")

....

a questo punto, forzando un errore qualsiasi (9 nel mio caso: "Solver encountered an error value in a target or constraint cell."), trovo:

answer = 9

ma la sub SolvErr non parte.

Qualche suggerimento?

grazie

domenico

Microsoft 365 e Office | Excel | Per la casa | Windows

Domanda bloccata. Questa domanda è stata eseguita dalla community del supporto tecnico Microsoft. È possibile votare se è utile, ma non è possibile aggiungere commenti o risposte o seguire la domanda.

0 commenti Nessun commento

Risposta accettata dall'autore della domanda

Anonimo
2017-05-08T16:36:07+00:00

Ciao Domenico,

    answer = SolverSolve(True, "ShowTrial")

Forse, prova:

         answer = SolvErr(SolverSolve(True, "ShowTrial"))

===

Regards,

Norman

La risposta è stata utile?

1 persona ha trovato utile questa risposta.
0 commenti Nessun commento

4 risposte aggiuntive

Ordina per: Più utili
  1. Anonimo
    2017-05-08T17:12:54+00:00

    Ciao Domenico

    Grande Norman!!!

    grazzzzzzieeeee!!!!

    Mi fa piacere che tu  abbia risolto il problema e ti ringrazio per il cortese riscontro.

    Per chiudere questo thread, vorrei chiederti gentilmente di contrassegnare la mia risposta come Risposta preferita. In questo modo, tu aiuterai anche coloro che potessero cercare soluzioni ai problemi simili negli archivi della Community.

    ===

    Regards,

    Norman

    La risposta è stata utile?

    0 commenti Nessun commento
  2. Anonimo
    2017-05-08T16:48:41+00:00

    Grande Norman!!!

    grazzzzzzieeeee!!!!

    cari saluti

    domenico

    La risposta è stata utile?

    0 commenti Nessun commento
  3. Anonimo
    2017-05-08T16:09:05+00:00

    Ciao e grazie per la risposta Norman.

    non le ho sottoposte in quanto ritenevo inutile, visto che il solver mi fornisce la soluzione corretta, comunque eccole:

    Sub Risolutore()

    Dim answer As Integer

        SolverReset

        SolverAdd CellRef:="$B$7:$B$11", Relation:=3, FormulaText:="0"

        SolverAdd CellRef:="$B$12", Relation:=2, FormulaText:="1"

        SolverAdd CellRef:="$B$14", Relation:=2, FormulaText:="$B$2"

        SolverOk SetCell:="$B$15", MaxMinVal:=2, ValueOf:=0, ByChange:="$B$7:$B$11", _

            Engine:=1, EngineDesc:="GRG Nonlinear"

        SolverOk SetCell:="$B$15", MaxMinVal:=2, ValueOf:=0, ByChange:="$B$7:$B$11", _

            Engine:=1, EngineDesc:="GRG Nonlinear"

        answer = SolverSolve(False, "SolvErr")

    'forzando un errore ottengo answer = 9, come detto, ma non viene eseguita la

    'Function SolvErr in nessun caso, anche se answer=0 (almeno questo ho capito leggendo qua e là)

    End Sub

    Function SolvErr(Reason As Integer)

    If Reason = 0 Then

        MsgBox ("Solver found a solution. All constraints and optimality conditions are satisfied.")

    End If

    If Reason = 1 Then

        MsgBox ("Solver has converged to the current solution. All constraints are satisfied")

    End If

    If Reason = 2 Then

        MsgBox ("Solver cannot improve the current solution. All constraints are satisfied.")

    End If

    If Reason = 3 Then

        MsgBox ("Stop chosen when the maximum iteration limit was reached.")

    End If

    If Reason = 4 Then

        MsgBox ("The Set Cell values do not converge.")

    End If

    If Reason = 5 Then

        MsgBox ("Solver could not find a feasible solution.")

    End If

    If Reason = 6 Then

        MsgBox ("Solver stopped at user's request.")

    End If

    If Reason = 7 Then

        MsgBox ("The conditions for Assume Linear Model are not satisfied.")

    End If

    If Reason = 8 Then

        MsgBox ("The problem is too large for Solver to handle.")

    End If

    If Reason = 9 Then

        MsgBox ("Solver encountered an error value in a target or constraint cell.")

    End If

    If Reason = 10 Then

        MsgBox ("Stop chosen when maximum time limit was reached.")

    End If

    If Reason = 11 Then

        MsgBox ("There is not enough memory available to solve the problem.")

    End If

    If Reason = 12 Then

        MsgBox ("Another Excel instance is using SOLVER.DLL. Try again later.")

    End If

    If Reason = 13 Then

        MsgBox ("Error in model. Please verify that all cells and constraints are valid.")

    End If

        ShowTrial = True

    End Function

    Cari saluti

    La risposta è stata utile?

    0 commenti Nessun commento
  4. Anonimo
    2017-05-08T15:22:12+00:00

    Ciao Domenico,

    ho creato una piccola macro che esegue calcoli con il solver.

    Il codice funziona perfettamente.

    Quello che non sono riuscito a fare è eseguire una seconda macro che in caso di errore mi segnali che tipo di errore c'è stato.

    Da quel che ho letto, dovrebbe farsi così:

    Mia Sub

    ....

    ....

    answer = SolverSolve(False, "SolvErr")

    ....

    a questo punto, forzando un errore qualsiasi (9 nel mio caso: "Solver encountered an error value in a target or constraint cell."), trovo:

    answer = 9

    ma la sub SolvErr non parte.

    Qualche suggerimento?

    Credo che ci sarebbe utile vedere le due macro.

    ===

    Regards,

    Norman

    La risposta è stata utile?

    0 commenti Nessun commento