在工作簿的每個頁面上,都有一個狀態欄,由狀態框組成。有三種狀態 - 「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
你還可以顯示調用'Sub StatusBar'的代碼嗎?我猜你是在沒有意識到的情況下進入遞歸。例如,你從一個'Worksheet_SelectChange'事件中調用,你的代碼改變了一些東西,並且你再次輸入事件,這是遞歸。 – Vityata
還有完整的錯誤信息? – ahmet
@ahmet「堆棧空間不足」是完整的錯誤消息。它在標題*和*問題的正文中。 –