1

在大型数据集上执行优化任务时,我不时收到溢出运行时错误 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 进行的。

4

2 回答 2

1

我认为这不一定是您的错误的原因,但无论如何都值得修复。

比较和对比代码的这两部分:

AddDataPoint

data_points.Add new_data_point
Set new_data_point = Nothing

在这里,我们将临时变量引用的对象添加new_data_point到集合中。然后我们设置new_data_pointNothing删除对它曾经引用的对象的引用。显然该集合仍然会引用该对象

Class_Terminate

For Each data_point In data_points  'destruct each data point individually
    Set data_point = Nothing
Next data_point

Here we are reading each item in turn from the collection into a temporary variable called data_point. We then set data_point to Nothing to remove the reference to the object that it used to refer to. (Maybe not quite so) obviously, the collection will still have a reference to this object.

To remove every object from the collection try repeatedly removing the first object in the collection until the collection is empty:

Do Until (data_points.Count < 1)
    data_points.Remove 1
Loop
于 2011-04-18T22:51:00.287 回答
0

在这种情况下,当您尝试进行超出分配目标限制的分配时,会导致溢出。您的数据集中是否有超出日期限制或可能是双倍限制的内容?这些是我看到的两种类型。也许某处不匹配,一个大的双倍作为日期被传入。您可以使用子例程检查此类内容,例如,在尝试将日期写入集合之前检查日期的范围。

另一种可能性是集合本身的大小,因为它是由 long 索引的。这是一个相当大的数字,您必须超过 2,147,483,647 条记录。

于 2011-04-18T17:41:25.590 回答