从 Excel 2003 升级到 2007 会导致“堆栈溢出错误” 在以前工作的vba上

发布于 2024-07-10 17:18:57 字数 639 浏览 6 评论 0原文

以下 VBA 代码在 Excel 2003 中运行良好,但在 Excel 2007 中会导致堆栈溢出错误。该代码需要根据下拉菜单选择解锁或锁定某些单元格。 我需要能够在 Excel 2003 和 2007 中运行代码。请帮忙。

Private Sub Worksheet_Change(ByVal Target As Range)
   If [E28] = "NO" Then
     ActiveSheet.Unprotect ("PASSWORD")
     [K47:K53].Locked = False
     [K47:K53].Interior.ColorIndex = 16
     [K47:K53].ClearContents
     ActiveSheet.Protect ("PASSWORD")
   Else
     ActiveSheet.Unprotect ("PASSWORD")
     [K47:K53].Interior.ColorIndex = 0
     'Next line is optional, remove preceding apostrophe if protection should stay on.
     ActiveSheet.Protect ("PASSWORD")
   End If
End Sub

The following VBA code works great in Excel 2003, but results in a Stack Overflow Error in Excel 2007. The code is required to either unlock or lock certain cells based on a drop-down menu selection. I need to be able to run the code in both Excel 2003 and 2007. Please help.

Private Sub Worksheet_Change(ByVal Target As Range)
   If [E28] = "NO" Then
     ActiveSheet.Unprotect ("PASSWORD")
     [K47:K53].Locked = False
     [K47:K53].Interior.ColorIndex = 16
     [K47:K53].ClearContents
     ActiveSheet.Protect ("PASSWORD")
   Else
     ActiveSheet.Unprotect ("PASSWORD")
     [K47:K53].Interior.ColorIndex = 0
     'Next line is optional, remove preceding apostrophe if protection should stay on.
     ActiveSheet.Protect ("PASSWORD")
   End If
End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

归属感 2024-07-17 17:18:57

堆栈溢出几乎肯定来自递归。 不确定为什么在 Excel 2003 中没有出现堆栈溢出 - 也许是在堆栈溢出之前引发了错误。

您可以防止无限递归,如下所示:

Private m_bInChange As Boolean

Private Sub Worksheet_Change(ByVal Target As Range)
If m_bInChange Then Exit Sub
On Error GoTo ErrHandler
    m_bInChange = True
    If [E28] = "NO" Then
        ActiveSheet.Unprotect ("PASSWORD")
        [K47:K53].Locked = False
        [K47:K53].Interior.ColorIndex = 16
        [K47:K53].ClearContents
        ActiveSheet.Protect ("PASSWORD")
    Else
        ActiveSheet.Unprotect ("PASSWORD")
        [K47:K53].Interior.ColorIndex = 0
        'Next line is optional, remove preceding apostrophe if protection should stay on.
        ActiveSheet.Protect ("PASSWORD")
    End If

    m_bInChange = False
    Exit Sub
ErrHandler:
    m_bInChange = False
    Exit Sub
End Sub

The stack overflow almost certainly comes from recursion. Not sure why you aren't getting a stack overflow in Excel 2003 - perhaps an error is being raised before the stack overflows.

You can protect against infinite recursion something like the following:

Private m_bInChange As Boolean

Private Sub Worksheet_Change(ByVal Target As Range)
If m_bInChange Then Exit Sub
On Error GoTo ErrHandler
    m_bInChange = True
    If [E28] = "NO" Then
        ActiveSheet.Unprotect ("PASSWORD")
        [K47:K53].Locked = False
        [K47:K53].Interior.ColorIndex = 16
        [K47:K53].ClearContents
        ActiveSheet.Protect ("PASSWORD")
    Else
        ActiveSheet.Unprotect ("PASSWORD")
        [K47:K53].Interior.ColorIndex = 0
        'Next line is optional, remove preceding apostrophe if protection should stay on.
        ActiveSheet.Protect ("PASSWORD")
    End If

    m_bInChange = False
    Exit Sub
ErrHandler:
    m_bInChange = False
    Exit Sub
End Sub
谁的年少不轻狂 2024-07-17 17:18:57

每当工作表中的任何单元格发生更改时,您的方法都会被调用。 您的“NO”块会更改目标单元格的内容,从而导致再次调用该方法。

选项:

  1. 按照 Joe 建议使用标志变量,看看您是否已经在执行
  2. 测试“目标”值,看看它是否是 E28,如果这是您想要捕获的单元格更改。 类似的东西

    如果目标.地址<> "$E$28" 然后 Exit Sub

Your method gets called whenever ANY cell in the worksheet changes. Your "NO" block changes the contents of the target cells, which causes the method to be called again.

Options:

  1. Use a flag variable as Joe suggested, to see if you're already executing
  2. Test the "Target" value to see if it's E28, if that's the cell change you want to capture. Something like

    If Target.Address <> "$E$28" Then Exit Sub

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文