vba - vba中发生意外错误(类型不匹配和无效的过程,调用或参数)

标签 vba excel error-handling

我有一个非常简单的模型,它可以运行多次;每次它重新计算工作表以生成结果,然后将这些结果粘贴到新工作表上,这样最后我就拥有大量的数据转储。

如果我将其设置为小于c.232000(每个模拟仅花费一秒钟的时间),它将运行良好,完全没有错误。

当我尝试运行超过c.232000 sims时,最终得到两个“类型不匹配”错误框,然后是“无效过程,调用或参数”错误框。在这三个错误上单击“确定”后,代码将继续执行并运行到最后。

没有人知道这些错误的可能原因,因为我发现sim 232000和232001之间没有区别?
我正在考虑使用“On Error/Resume Next”,但是我需要知道原因在哪里;无论是Activesheet.calculate行还是复制行和过去的行。

Sub simdump()

Application.ScreenUpdating = True
Application.DisplayAlerts = False

Sheets("Dump").Select
Range("G1").Value = Format(Now(), "yyyy-MM-dd hh:mm:ss")
Range("A3:G500000").ClearContents
Range("I3:IY500000").ClearContents

Sheets("Cost").Select
Range("E2").Value = 1

Dim nScenarios As Integer
Dim simMultiplier As Integer

nScenarios = Application.WorksheetFunction.CountA(Sheets("Scenarios").Range("D:D")) - 1
simMultiplier = Sheets("Cost").Range("D51").Value

For k = 1 To nScenarios

Sheets("Scenarios").Select
Range(Cells(k + 2, 1), Cells(k + 2, 3)).Select
Selection.Copy
Sheets("Loans").Select
Range("N4").Select
Selection.PasteSpecial Paste:=xlPasteValues

Sheets("Scenarios").Select
Cells(k + 2, 4).Select
Selection.Copy
Sheets("Cost").Select
Range("E6:CZ6").Select
Selection.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False

Sheets("Loan Backup").Select
Range("K2:K250").FormulaR1C1 = "=RC[1]-Scenarios!R" & k + 2 & "C5"
ActiveSheet.Calculate


For n = 1 To simMultiplier
    Sheets("Cost").Select
    Range("E53:IT352").ClearContents

    For l = 1 To 3
        Sheets("Loan Backup").Select
        Range(Cells((l - 1) * 100 + 2, 1), Cells(l * 100 + 1, 11)).Select
        Selection.Copy
        Sheets("Loans").Select
        Range("A2").Select
        Selection.PasteSpecial Paste:=xlPasteValues

        Sheets("Correlation").Calculate

        Sheets("Cost").Select

        For m = 1 To 300

            ActiveSheet.Calculate

            Range("E46:CZ46").Select
            Selection.Copy
            Cells(52 + m, 5 + (l - 1) * 100).Select
            Selection.PasteSpecial Paste:=xlPasteValues
            Application.CutCopyMode = False
        Next m
    Next l
    Range("D53:IT352").Copy
    Sheets("Dump").Select
    Cells(300 * ((n - 1) + (k - 1) * simMultiplier) + 3, "I").Select
    Selection.PasteSpecial Paste:=xlPasteValues

    ActiveWorkbook.Save

Next n
Cells(300 * (k - 1) * simMultiplier + 3, "G").Value = Format(Now(), "yyyy-MM-dd hh:mm:ss")

Sheets("Scenarios").Select
Range(Cells(k + 2, 1), Cells(k + 2, 5)).Select
Selection.Copy
Sheets("Dump").Select
Cells(300 * (k - 1) * simMultiplier + 3, "B").Select
Selection.PasteSpecial Paste:=xlPasteValues

Range(Cells(300 * (k - 1) * simMultiplier + 3, "A"), Cells(300 * k * simMultiplier + 2, "A")).Value = k

Next k

Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub

最佳答案

也许这与工作表大小的限制有关。
工作表大小的限制为:1,048,576行x 16,384列source
因此,如果232000次迭代都产生5个值放在一列中,则您超过了行数。

并检查它是否与迭代次数有关,或者与特定的迭代次数有关,您可以不是从1开始运行,而是在通常发生错误的次数附近开始运行,因此它只能运行几分钟。

关于vba - vba中发生意外错误(类型不匹配和无效的过程,调用或参数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31262378/

相关文章:

excel - 如何在不更改的情况下将 csv 表复制到 excel 文件中

vb.net - 如何在不打开的情况下创建 Excel 文件的副本

vba - 失去与SharePoint问题的连接

excel - 使用复制目标和选择性粘贴来保持格式

excel - Access VBA : Date function not working

excel - VBA - 在 Evaluate() 中包含自己的函数

vba - Excel VBA 使用 Workbook.Open 并显示 Dir(Directory) 的结果

excel - 保存工作簿的 vba 例程没有预期的结果

java - 虚方法错误和NullPointerException错误

php - 如何在 PHP 中获取有用的错误消息?