在大型数据集上执行优化任务时,我不时收到溢出运行时错误 6(通常在 1 小时或 2 小时后)。当我从停止的地方重新启动宏时,错误消失了,即从发生错误的地方再次启动宏。溢出错误是否与创建了太多使用后未正确销毁的对象有关?
这是我的容器类的(简化版本),它被破坏(通过 Set ... = nothing)和重建(通过 Set ... = New)数千次。
'CG_data_point custom collection class
Public data_points As Collection
Private Sub Class_Initialize()
Set data_points = New Collection
End Sub
Public Sub AddDataPoint(mydate as date, price as double)
Dim new_data_point As CG_data_point
Set new_data_point = New CG_data_point
new_data_point.EnterData mydate, price
data_points.Add new_data_point
Set new_data_point = Nothing 'I assume this one could also be skipped
End Sub
Public Sub RMSE(X as double) as double
...
End Sub
Private Sub Class_Terminate()
Dim data_point As CG_data_point
For Each data_point In data_points 'destruct each data point individually
Set data_point = Nothing
Next data_point
Set data_points = Nothing
End Sub
'Main module
dim global_container as CG_data_container
sub do_optimizations()
Do
set global_container= new CG_data_container
.... do something with the data, have in call to global function RMSE_UDF as a cell formula for Solver
set global_container= nothing
While (...)
end sub
'worksheet function
function RMSE_UDF(X as double)
global_container.RMSE(X)
end function
容器变量 global_container 必须是全局的,因为它必须可以从工作表 UDF (RMSE_UDF) 中调用;据我所知,工作表公式不能将对象作为参数,例如“=RMSE(MyContainer,...)”。均方根误差 (RMSE) 的最小化是使用 Excel Solver 进行的。