取消隐藏非常隐藏的工作表 Excel VBA
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24272742/
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
Unhiding very hidden sheet Excel VBA
提问by nickalbe
I am trying to create a user form that will unhide a specific worksheet based on the value of the combo box on the user form. There are 3 different worksheets that are " very hidden" in the workbook. The combo box is populated with 3 choices, one for each hidden sheet. I am using select case to make the correct sheet visible (Eventually there will be many more than 3 sheets/options. Sample code follows (located in the user form code window):
我正在尝试创建一个用户表单,该表单将根据用户表单上组合框的值取消隐藏特定工作表。工作簿中有 3 个不同的工作表“非常隐藏”。组合框包含 3 个选项,每个隐藏表一个。我正在使用 select case 使正确的工作表可见(最终将有超过 3 个工作表/选项。示例代码如下(位于用户表单代码窗口中):
Private Sub NextButton_Click()
Select Case ComboBox
Case ComboBox.ListIndex = 0
Sheets(1).Visible = True
Case ComboBox.ListIndex = 1
Sheets(2).Visible = True
Case ComboBox.ListIndex = 2
Sheets(3).Visible = True
End Select
Unload UserForm
End Sub
I click the next button, the userform unloads, but the sheets does not become visible. VBA brings up no errors either. Please let me know if I need to provide any more information.
我单击下一步按钮,用户窗体卸载,但工作表不可见。VBA 也不会出现任何错误。如果我需要提供更多信息,请告诉我。
Nik
尼克
回答by mason
Your case statement is incorrect. You should tell it what value to test in the first part, then specify the values later. See Tech on the Net article about Case in VBA.
您的案例陈述不正确。您应该在第一部分告诉它要测试的值,然后再指定这些值。请参阅有关 VBA 中案例的网络技术文章。
Private Sub NextButton_Click()
Select Case ComboBox.ListIndex
Case 0
Sheets(1).Visible = True
Case 1
Sheets(2).Visible = True
Case 2
Sheets(3).Visible = True
End Select
Unload UserForm
End Sub
This is how case statements in most programming languages work.
这就是大多数编程语言中的 case 语句的工作方式。
However, since ComboBox.ListIndex
is an int, and you're telling it what sheet to show based on that, you could simplify the whole thing and drop the case statement. This presumes the indexes match up, which they do in your example.
但是,由于ComboBox.ListIndex
是一个 int,并且您告诉它基于该显示哪个工作表,您可以简化整个事情并删除 case 语句。这假定索引匹配,它们在您的示例中是这样做的。
Private Sub NextButton_Click()
Sheets(ComboBox.ListIndex+1).Visible = True
Unload UserForm
End Sub