2016-11-06 67 views
0

當代碼運行時,出現「堆棧空間不足」錯誤(28)。通常它會完全崩潰Excel,但有時我可以在打開工作表時立即「調試」。當爲循環指定命名範圍時Excel VBA'堆棧空間不足'

當它這樣做時,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個細胞在這個範圍內是不爲空 - 但我不知道它可能是什麼其他

+0

你確定Mail_adv_liq_reminder不是罪魁禍首嗎?你的問題是不可複製的,因此其他人很難做出更多猜測。請參閱http://stackoverflow.com/help/mcve –

+4

您可以嘗試'Application.EnableEvents = False'作爲事件處理程序的第一行。這將阻止事件處理程序調用的級聯,這可能是問題所在。 –

回答

1

.Offset(0, 3).Value = "Not numeric"可能會觸發一個計算,即使沒有明確提到它的單元也許喲你有一些易變的公式。由於您使用Application.EnableEvents = False,您似乎意識到無限迴歸的危險,但只能在代碼的一個分支中使用。幾乎可以肯定的是,代碼的其他分支正在觸發計算。另一種可能性(我知道的)是sub Mail_adv_liq_reminder以某種方式觸發計算作爲副作用。行Set FormulaRange = Me.Range("M3:M61")更有可能不是症狀而不是原因。做這個任務的嘗試恰好是打破駱駝背的秸稈。

解決方案是將無條件Application.EnableEvents = False作爲事件處理程序的第一行(在最後將其設置回true)。

+0

感謝您的反饋。由於我試圖修改我在別處找到的代碼,因此Application.EnableEvents = False行的出現只反映了原始代碼(可能是Rob de Bruin)編寫者的智慧,而不是我的深入洞察力。 – TSlade