隐藏/取消隐藏特定工作表基于按钮单击

时间:2016-12-30 06:14:50

标签: excel-vba vba excel

我的工作簿Sample中有10个工作表 在摘要工作表中,我需要指定一个宏来隐藏和取消隐藏特定的工作表,如上图所示。

如果点击Button1,则必须隐藏Button2下面列出的所有表格。同样,如果我点击Button2,则必须隐藏Button1下列出的所有工作表。

有人可以帮我写这个VBA代码吗?

我尝试使用下面的代码,但它不支持我2个按钮:

Sub ShowHideWorksheets()

  Dim Cell As Range
  For Each Cell In Range("B6:B7")

    ActiveWorkbook.Worksheets(Cell.Value).Visible = Not ActiveWorkbook.Worksheets(Cell.Value).Visible

  Next Cell

End Sub

3 个答案:

答案 0 :(得分:0)

在Visual Basic窗口中,尝试点击右上角的第二个组合框。

在工作表中添加按钮时,它应该为您添加一个" button1.click"功能如下(法语:" Bouton1_Cliquer")。 Combo box picture

然后,您只需要复制此Sub中的代码,并将其调整为第二个按钮(也可能出现在组合框中)。 它将完成这项工作。

答案 1 :(得分:0)

试试这个:

print_r($newArray);

您需要为第二个按钮创建类似的功能,但具有不同的范围(可能在C列中)。

答案 2 :(得分:0)

如果工作表不存在,建议您使用错误处理。

Sub Button1()

Dim rng1 As Range

On Error Resume Next

For Each rng1 In Range("B6:B" & Range("B" & Rows.Count).End(xlUp).Row)
        Sheets(rng1.Value).Visible = False
Next rng1
End Sub


Sub Button2()

Dim rng1 As Range

On Error Resume Next
For Each rng1 In Range("A6:B" & Range("A" & Rows.Count).End(xlUp).Row)
        Sheets(rng1.Value).Visible = False
Next rng1

End Sub
相关问题