在为循环指定命名范围时,Excel VBA'Stack out of Space'

时间:2016-11-06 13:50:30

标签: excel-vba stack-overflow vba excel

当此代码运行时,我收到“Stack out of Space”错误(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个细胞不是空白的 - 但我不知道它还能做什么。

1 个答案:

答案 0 :(得分:1)

var sampleArray = [{ "Name": "A", "IsImportant": true }, { "Name": "B", "IsImportant": true }, { "Name": "C", "IsImportant": false }, { "Name": "D", "IsImportant": false }, { "Name": "E", "IsImportant": false },, { "Name": "F", "IsImportant": true }, , { "Name": "G", "IsImportant": true }, { "Name": "H", "IsImportant": false }]; var results = sampleArray.filter(x => x.IsImportant).concat(sampleArray.filter(x => !x.IsImportant)); console.log(results);可能会触发计算,即使没有明确引用它的单元格也是如此。也许你有一些不稳定的公式。您似乎意识到自使用.Offset(0, 3).Value = "Not numeric"后无限回归的危险,但仅在代码的一个分支中。几乎可以肯定的是,代码的其他分支正在触发计算。另一种可能性(据我所知)是,子Application.EnableEvents = False以某种方式触发计算作为副作用。行Mail_adv_liq_reminder更可能不是症状而不是原因。尝试做这项任务恰好是打破骆驼背部的稻草。

解决方案是将无条件Set FormulaRange = Me.Range("M3:M61")作为事件处理程序的第一行(在结尾时将其设置为等于true)。