使用 VBA 在 Excel 中隐藏和取消隐藏选定的工作表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10459582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hide and unhide selected sheets in Excel using VBA
提问by NCC
Problem: I would like to have the ability to hide and unhide selected (multiple) sheets by a form
问题:我希望能够通过表单隐藏和取消隐藏选定的(多个)工作表
Available Resource: There many available resources that show how to unhide and hide all sheets at one but not flexible
可用资源:有许多可用资源显示了如何在一张纸上取消隐藏和隐藏所有工作表,但不灵活
Explain:
解释:
Because sheet5 is hidden checkbox corresponds to Sheet5 is checked.
因为 sheet5 被隐藏复选框对应于 Sheet5 被选中。
Logic/approach:
逻辑/方法:
- Get all of the sheets' name include hided ones and bind them to Labels, check if it is hide or unhide and bind it to checkbox.
- 获取所有工作表的名称,包括隐藏的并将它们绑定到标签,检查它是隐藏还是取消隐藏并将其绑定到复选框。
Label and Checkbox are created automatically, lable and checkbox is somehow linked to each other so the program knows which sheets to hide and to unhide.
标签和复选框是自动创建的,标签和复选框以某种方式相互链接,因此程序知道要隐藏和取消隐藏哪些工作表。
- write the code to check whether a checkbox changes its status between selected/unselected.
- 编写代码以检查复选框是否在选中/未选中之间更改其状态。
回答by NCC
At last I am able to do the work. It is probably not good code, but it works.
我终于可以做这项工作了。这可能不是好的代码,但它有效。
Private Sub btListAllSheets_Click()
With Me.ListBox1
.Clear
.ColumnHeads = True
.ColumnCount = 2
Dim status As String
For i = 1 To Sheets.Count
If Sheets(i).Visible = xlSheetHidden Then
status = "Invisible"
Else
status = "Visible"
End If
ListBox1.AddItem (Sheets(i).Name)
ListBox1.List(ListBox1.ListCount - 1, 1) = status
Next i
End With
End Sub
Private Sub bt_hideunhideselectedsheet_Click()
Dim str As String
str = Me.ListBox1.Column(1, Me.ListBox1.ListIndex)
For Each Sh In ThisWorkbook.Worksheets
If Sh.Name = Me.ListBox1.Value And str = "Visible" Then
Sh.Visible = False
ElseIf Sh.Name = Me.ListBox1.Value And str = "Invisible" Then
Sh.Visible = True
End If
Next Sh
End Sub