以下 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