Excel VBA-当被调用方法已经调用了另一个方法时,将控制权返回给调用方法

时间:2019-05-16 18:40:32

标签: excel vba oop error-handling event-handling

我有一个用Excel VBA编码的模型仿真。它内置在名为“ ChemicalRelease”的类模块内部。还有一个名为“ UniversalSolver”的Class模块,用于优化ChemicalRelease的参数。

在运行不同的仿真时,UniversalSolver有时会使用超出应用程序建模范围的参数组合。由于它基于参数的多个组合,因此很难确定真实的建模边界。

UniversalSolver的实例将创建一组输入参数,并实例化ChemicalRelease以按照指定的方式运行模型。在ChemicalRelease内部,该流在几种方法(例如“ setden”)中起作用,这些方法可以调用其他方法来执行其计算。例如,“ setden”可以调用“ tprop”来确定热力学性质,而“ tprop”可以依次调用一个函数来迭代求解值。

在任何这些方法中的任何时候,模型都可以确定无法求解输入参数的组合。当前配置通过msgbox通知我该问题,并停止程序,使其进入调试模式。

我想利用一个事件处理程序,该事件处理程序将设置一个处理程序实例的值,该实例将在“ ChemicalRelease”中停止计算,将该实例设置为“ Nothing”,并将控件直接返回到“ UniversalSolver”,实例化“ ChemicalRelease”并进行建模的行。

服务器式Google搜索,没有一个方法可以将控制权返回给“ UniversalSolver”。

'事件处理程序代码:记入Change in variable triggers an event

“ ClassWithEvent”类

Public Event VariableChange(value As Integer)
Private p_int As Integer
Public Property Get value() As Integer
    value = p_int
End Property
Public Property Let value(value As Integer)
    If p_int <> value Then RaiseEvent VariableChange(value) 'Only raise on 
    actual change.
    p_int = value
End Property

“ ClassHandlesEvent”类

Private WithEvents SomeVar As ClassWithEvent
Private Sub SomeVar_VariableChange(value As Integer) 'This is the event 
    handler.

    'line here to return control to "UniversalSolver" instance, out of 
     "ChemicalRelease" instance, regardless of how many methods have to be 
     returned out of within ChemicalRelease.

End Sub
Public Property Get EventVariable() As ClassWithEvent
    Set EventVariable = SomeVar
End Property
Public Property Let EventVariable(value As ClassWithEvent)
    Set SomeVar = value
End Property

“全局”模块 “为ClassHandlesEvent和ClassWithEvent全局设置实例

Global VAR As ClassHandlesEvent
Global TST As ClassWithEvent

“ UniversalSolver”类

Public Sub initialize()

    Set VAR = New ClassHandlesEvent
    Set TST = New ClassWithEvent
    VAR.EventVariable = TST

End Sub

Public Sub solve()

     Do 'iterate through potential input parameters

         Set m_chemRelease = New ChemicalRelease

         m_chemRelease.initialize 'initializes and launches modeling

     Loop until satisfied

End Sub

“化学释放”类

Public Sub initialize(modelParamsSheet As Worksheet)

    Set m_modelParamsSheet = modelParamsSheet
    Call readModelInputsAndSetProperties(0)


End Sub

Private Sub readModelInputsAndSetProperties(inNum As Integer)

    'set all properties and launch modeling
    Call setjet(0)

End Sub

Private Sub setjet(inInt As Integer)

    'lots of math.  

    call tprop(tpropsInputDict)

    'lots more math.



End Sub

Private Sub tprop(inDict as Scripting.Dictionary)

    'more math.

    'check for convergence

    If check > 0.00001 Then

        'failed convergence
        'trigger event to exit ChemicalRelease Instance and return control 
         to UniversalSolver instance

        TST.value = 2


    End If

    'more math.

    Call limit()

End Sub

Private Sub limit()

    'more math.

    'check for sign

    If fa * fb > 1 Then

        'failed convergence
        'trigger event to exit ChemicalRelease Instance and return control 
         to UniversalSolver instance

        TST.value = 2


    End If

    'more math.

End Sub

预期结果将产生一个事件,该事件可以在项目中的任何位置触发,将把控制权返回给UniversalSolver,就像我在ChemicalRelease.initialize中声明“退出子”一样。但是,我找不到有效的方法。

1 个答案:

答案 0 :(得分:0)

调用函数中的错误处理适用于所有被调用函数。但是,需要“ resume”命令才能使VBA退出错误处理模式。按照下面的代码,流程将在调用函数的“ endoffor”标签处返回正常模式。

errcatch:
Err.Clear

On Error GoTo errcatch
Resume endoffor '