【问题标题】:Excel VBA 'Stack out of Space' when assigning named range for loop为循环分配命名范围时,Excel VBA“堆栈空间不足”
【发布时间】:2016-11-06 22:42:12
【问题描述】:

运行此代码时,我收到“堆栈空间不足”错误 (28)。通常它会使 Excel 完全崩溃,但有时我可以在打开工作表后立即“调试”。

当它出现时,VBA 编辑器会突出显示Set FormulaRange = Me.Range("M3:M61") 行。

我认为问题可能是由于 "M3:M61" 范围实际上是 Excel 表中的一个命名范围(如下一行 'Set FormulaRange = Workbooks("Advance Request & Tracking Form rebuild.xlsm").Range("tbl_interface[Liquidation in]").RefersToRange 所示,我已将其注释掉,因为我永远无法让它工作。

    Option Explicit
Private Sub Worksheet_Calculate()
    Dim FormulaRange As Range
    Dim countdownRange As Range
    Dim NotSentMsg As String
    Dim MyMsg As String
    Dim SentMsg As String
    Dim MyLimit As Double
    Dim countdownFeedbackOffset As Double

    NotSentMsg = "Not Sent"
    SentMsg = "Sent"

    'Below the MyLimit value it will run the macro
    MyLimit = 1

    'Set the range with Formulas that you want to check
    Set FormulaRange = Me.Range("M3:M61")
    'Set FormulaRange = Workbooks("Advance Request & Tracking Form rebuild.xlsm").Range("tbl_interface[Liquidation in]").RefersToRange

    'MsgBox FormulaRange

    countdownFeedbackOffset = 2

    On Error GoTo EndMacro:
    For Each FormulaCell In FormulaRange.Cells
        With FormulaCell
            If IsNumeric(.Value) = False Then
                .Offset(0, 3).Value = "Not numeric"
                MyMsg = "Not numeric"
            Else
                If .Value < MyLimit Then
                    MyMsg = SentMsg
                    If .Offset(0, countdownFeedbackOffset).Value = NotSentMsg Then
                        Call Mail_adv_liq_reminder
                        'Call Mail_with_outlook1
                    End If
                Else
                    MyMsg = NotSentMsg
                End If
            End If
            Application.EnableEvents = False
            .Offset(0, countdownFeedbackOffset).Value = MyMsg
            Application.EnableEvents = True
        End With
    Next FormulaCell

ExitMacro:
    Exit Sub

EndMacro:
    Application.EnableEvents = True

    MsgBox "Some Error occurred." _
         & vbLf & Err.Number _
         & vbLf & Err.Description
End Sub

这让我有点难以置信,它可能会窒息 - 该范围内实际上只有 6 个不是空白的单元格 - 但我不知道它还能是什么。

【问题讨论】:

  • 你确定Mail_adv_liq_reminder 不是罪魁祸首吗?您的问题是不可重现的,因此其他人除了猜测它是什么之外很难做任何事情。见stackoverflow.com/help/mcve
  • 您可以尝试将Application.EnableEvents = False 作为事件处理程序的第一行。这将防止级联事件处理程序调用,这可能是问题。

标签: excel stack-overflow vba


【解决方案1】:

.Offset(0, 3).Value = "Not numeric" 可能会触发计算,即使没有明确引用它的单元格。也许你有一些不稳定的公式。自从您使用Application.EnableEvents = False 以来,您似乎意识到了无限倒退的危险,但仅在代码的一个分支中。几乎可以肯定的是,代码的其他分支正在触发计算。另一种可能性(据我所知)是子Mail_adv_liq_reminder 以某种方式触发计算作为副作用。事实上,Set FormulaRange = Me.Range("M3:M61") 行更有可能不是症状而不是原因。尝试完成这项任务恰好是压垮骆驼的最后一根稻草。

解决方案是使用无条件的Application.EnableEvents = False 作为事件处理程序的第一行(最后将其设置为 true)。

【讨论】:

  • 感谢您的反馈。由于我正在尝试修改在其他地方找到的代码,因此 Application.EnableEvents = False 行的存在仅反映了编写原始代码的人(可能是 Rob de Bruin)的智慧,而不是我的洞察力。
猜你喜欢
  • 1970-01-01
  • 2017-11-14
  • 2015-03-31
  • 2016-01-20
  • 2018-08-07
  • 1970-01-01
  • 2015-08-03
  • 1970-01-01
  • 2019-12-05
相关资源
最近更新 更多