2017-09-20 43 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