2017-09-20 76 views
-1

在工作簿的每個頁面上,都有一個狀態欄,由狀態框組成。有三種狀態 - 「Tab Started」,「Design Updated」和「Configurations Complete」。最初,我在每個頁面上調用了這些框(並使用絕對引用),但我最近試圖通過將代碼移動到單獨的模塊並在頂部附近的每個工作簿頁面上調用它來提高工作簿的效率和靈活性(+使用「查找」而不是絕對引用來設置變量)。「堆棧空間不足」錯誤不一致

但是,雖然這個工作時間超過90%或更多,偶爾我會收到一條錯誤消息「Out of Stack Space」。閱讀MSDN,沒有任何可能觸發此錯誤的示例似乎適用於我的代碼(例如代碼不會自行調用)。

查看下面的代碼。

'This function is called by all workbook tabs and controls the status boxes 

Sub StatusBars(ByVal Target As Range) 

Dim TabStarted1 As Range 
Set TabStarted1 = ActiveSheet.Range("A4:Z5").Find("Tab Started") 
Dim TabStarted As Range 
Set TabStarted = TabStarted1.Offset(0, 1) 

Dim Design1 As Range 
Set Design1 = ActiveSheet.Range("A6:Z7").Find("Design Updated") 
Dim Design As Range 
Set Design = Design1.Offset(0, 1) 

Dim Configurations1 As Range 
Set Configurations1 = ActiveSheet.Range("A8:Z9").Find("Configurations Complete") 
Dim Configurations As Range 
Set Configurations = Configurations1.Offset(0, 1) 

If Not Intersect(Target, TabStarted) Is Nothing Then 
    If Target.Cells.Count = 2 Then 
     If WorksheetFunction.CountA(Target) = 0 Then 'If box is empty, then add an X, format it, change the box color and the tab color 

      TabStarted.Value = "X" 
      TabStarted.HorizontalAlignment = xlCenter 
      TabStarted.Font.Size = 25 
      TabStarted.Interior.Color = RGB(255, 255, 0) 
      Design.Interior.Color = RGB(255, 255, 255) 
      Design.Value = "" 
      Configurations.Interior.Color = RGB(255, 255, 255) 
      Configurations.Value = "" 
      ActiveSheet.Tab.Color = RGB(255, 255, 0) 

     Else 'if box is already checked clear, the X, the color, and the tab color 
      TabStarted.Interior.Color = RGB(255, 255, 255) 
      TabStarted.Value = "" 
      ActiveSheet.Tab.ColorIndex = xlColorIndexNone 
     End If 
    End If 

End If 

If Not Intersect(Target, Design) Is Nothing Then 
    If Target.Cells.Count = 2 Then 
     If WorksheetFunction.CountA(Target) = 0 Then 
      Design.Value = "X" 
      Design.HorizontalAlignment = xlCenter 
      Design.Font.Size = 25 
      Design.Interior.Color = RGB(0, 112, 192) 
      TabStarted.Interior.Color = RGB(255, 255, 255) 
      TabStarted.Value = "" 
      Configurations.Interior.Color = RGB(255, 255, 255) 
      Configurations.Value = "" 
      ActiveSheet.Tab.Color = RGB(0, 112, 192) 

     Else 
      Design.Interior.Color = RGB(255, 255, 255) 
      Design.Value = "" 
      ActiveSheet.Tab.ColorIndex = xlColorIndexNone 
     End If 
    End If 

End If 

If Not Intersect(Target, Configurations) Is Nothing Then 
    If Target.Cells.Count = 2 Then 
     If WorksheetFunction.CountA(Target) = 0 Then 
      Configurations.Value = "X" 
      Configurations.HorizontalAlignment = xlCenter 
      Configurations.Font.Size = 25 
      Configurations.Interior.Color = RGB(0, 176, 80) 
      TabStarted.Interior.Color = RGB(255, 255, 255) 
      TabStarted.Value = "" 
      Design.Interior.Color = RGB(255, 255, 255) 
      Design.Value = "" 
      ActiveSheet.Tab.Color = RGB(0, 176, 80) 

     Else 
      Configurations.Interior.Color = RGB(255, 255, 255) 
      Configurations.Value = "" 
      ActiveSheet.Tab.ColorIndex = xlColorIndexNone 
     End If 
    End If 

End If 

End Sub 

編輯: 調用此函數的代碼的一個例子:

'Remove Case Sensitivity 
    Option Compare Text 

Private Sub Worksheet_SelectionChange(ByVal Target As Range) 

Application.ScreenUpdating = False 


Dim var1 As Variant 
Dim var2 As Variant 
Dim var3 As Variant 

Dim PlusTemplates As Range 
Set PlusTemplates = Range("A14:Z15").Find("+") 

Call StatusBars(Target) 

[rest of the code] 
Application.ScreenUpdating = True 
End Sub 
+1

你還可以顯示調用'Sub StatusBar'的代碼嗎?我猜你是在沒有意識到的情況下進入遞歸。例如,你從一個'Worksheet_SelectChange'事件中調用,你的代碼改變了一些東西,並且你再次輸入事件,這是遞歸。 – Vityata

+0

還有完整的錯誤信息? – ahmet

+0

@ahmet「堆棧空間不足」是完整的錯誤消息。它在標題*和*問題的正文中。 –

回答

0

Activesheet是一個全局變量。當你使用Activesheet設置tabstarted1等時,因爲activesheet在堆棧上並且沒有處理,你的其他變量如tabstarted1,design1會留在堆棧內存中。嘗試獲取activesheet作爲你的函數的參數。

+0

會是什麼樣子?如果我通過「Call StatusBars(Target,ActiveSheet)」調用它,我是否需要將Sub聲明爲:Sub StatusBars(ByVal Target As Range,ActiveSheet As Worksheet)?這對變量本身意味着什麼 - 我仍然會需要在我的「查找」中指定ActiveSheet.Range? – Mknerr

+0

我不知道記住的語法,但它可以proply當你寫在評論中。我不確定,它是否修復,但當你嘗試重構時,你也應該這樣做來封裝你的變量。 – ahmet

0

我認爲這個錯誤是因爲你的代碼改變了工作表並因此調用了一個新的事件。爲了確保是這種情況,請執行下列操作 - 插入在這樣的情況下一個STOP:

Private Sub Worksheet_SelectionChange(ByVal Target As Range) 

    Application.ScreenUpdating = False 
    Stop 

    Dim var1 As Variant 
    'rest of your code. 
End Sub 

第一次你有一個選擇的變化,你會停止的停止。然後按F5並繼續。如果再次停止,那麼這是一個遞歸錯誤。

解決問題的最簡單方法是在事件開始時使用Application.EnableEvents = False,在代碼結束時使用Application.EnableEvents = True

+0

這似乎有助於解決堆棧空間問題,但現在如果發生任何其他問題,我的Excel基本上被凍結,因爲EnableEvents尚未重新啓用。在我調用StatusBars()函數後立即使用.EnableEvents = True是否有缺點?我還在偶爾出現錯誤(我相信它是「With block variable not set」錯誤),它突出顯示了函數中的「Set TabStarted = TabStarted1.Offset(0,1)」行。 – Mknerr