在工作簿的每个页面上,都有一个状态栏,由状态框组成。有三种状态 - “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“堆栈空间不足”是完整的错误消息。它在标题*和*问题的正文中。 –