在 Excel 中,更新公式似乎会触发同一工作表上不相关的私有子项。我怎样才能让它停止?

In Excel, updating a formula seems to trigger an unrelated private sub on the same worksheet. How can I get this to stop?

我通过工作簿导航的数据验证创建了一个下拉菜单。以下是我为下拉框更改工作簿中的工作表的代码片段:

Private Sub Worksheet_Change(ByVal Target As Range)
   On Error Resume Next
    If Not (Application.Intersect(Range("J4"), Target) Is Nothing) Then _
        ThisWorkbook.Sheets("Home").Visible = False
        ThisWorkbook.Sheets(Target.Value).Activate
        ThisWorkbook.Sheets(Target.Value).Visible = True
        ThisWorkbook.Sheets("Imported Data").Visible = False
End Sub

该代码旨在隐藏除所选工作表之外的下拉列表可访问的所有其他工作表。我有大约 10 个选项卡,这段代码完美地实现了导航的基本目标。但是,有些页面有公式,当您更新用于计算的单元格中的数据时,工作簿会跳转到工作簿中的随机工作表,该工作簿根本没有在此子中引用。

有什么方法可以让我的工作表不尝试对这个子项做任何事情,除非下拉菜单本身发生变化吗?

奖金(不太重要)问题:有没有办法让下拉框默认为(空白)除非菜单本身被访问了?

Then _

  1. space 后跟下划线 _ 表示当前语句尚未完成,但会在下一行继续。现在,只要工作发生变化,最后 3 行就会 运行sheet。将整个代码放在 If-Endif.

  2. 同时避免不必要地使用 On Error Resume Next。使用正确的错误处理。

  3. 您需要在激活 sheet 之前使其可见,而不是相反。

试试这个

Private Sub Worksheet_Change(ByVal Target As Range)
    On Error GoTo Whoa
    
    If Not (Application.Intersect(Range("J4"), Target) Is Nothing) Then
        ThisWorkbook.Sheets("Home").Visible = False
        
        ThisWorkbook.Sheets(Target.Value).Visible = True
        ThisWorkbook.Sheets(Target.Value).Activate
        
        ThisWorkbook.Sheets("Imported Data").Visible = False
    End If
    
Letscontinue:
    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume Letscontinue
End Sub

is there a way to make the drop box default to (blank) unless the menu itself is accessed?

如果您是使用数据验证创建的,则在列表中插入一个空白值。