我是 VBA 的新手,试图破译函数并构建交互式文件。在一位用户的帮助下(他肯定知道他是谁;)),我可以学到很多东西。
现在我陷入了一段荒谬的代码:我想 1/取消隐藏一张工作表,2/转到该工作表,3/当用户选择“返回起始页-”时重新隐藏该工作表按钮”。 所以我编写了这段代码:
Sub FRtoEN()
'
' FRtoEN Macro
' Emmène au Glossaire FR ==> EN
'
Sheets("synthèse_FR&EN").Visible = True
Sheets("Synthèse_FR&EN").Select
End Sub
而且效果很好。但我不知道如何用 VBA 语言告诉 excel 我希望它在用户完成并单击退出按钮后重新隐藏选项卡。
你能帮我吗?
最佳答案
当您想要使用一个按钮来首先显示隐藏的工作表,并且下次单击同一个按钮时,它会隐藏同一个工作表时,Ferndiando 的答案非常出色。
制作一个按钮显示一张工作表,另一个按钮隐藏同一张工作表,请执行以下操作;
在第一个按钮中,您将使代码可见:
Sub FRtoEN()
'
' FRtoEN Macro
' Emmène au Glossaire FR ==> EN
'
Sheets("synthèse_FR&EN").Visible = True
Sheets("Synthèse_FR&EN").Activate
End Sub
在将用户带回“主页”的第二个按钮中,您可以添加以下代码:
Sub StartPage()
Sheets("Start Page").Activate 'First go to Start page
Sheets("synthèse_FR&EN").Visible = False 'Then hide the sheet they currently visited, that makes the experience a little bit more "working in background"
End Sub
如果我假设您对多个工作表使用此“返回起始页按钮”,那么每次有人进入起始页时,您也可以隐藏其他工作表。
Sub StartPage()
Sheets("Start Page").Activate 'First go to Start page
Sheets("synthèse_FR&EN").Visible = False
Sheets("synthèse_FR&DE").Visible = False 'Example 1 - No matter which sheet you visit, it will hide this sheets.
Sheets("synthèse_FR&SP").Visible = False 'Example 2 - No matter which sheet you visit, it will hide this sheets.
End Sub
如果您希望代码在隐藏工作表上执行操作,而它们仍然对用户隐藏(例如后台过滤/计算/复制数据等..),这将为用户提供流畅的体验:
Sub StartPage()
Application.ScreenUpdating = False 'Turn of all visual updates the macro does. Macro works in background without showing every step visually in Excel.
Sheets("synthèse_FR&EN").Visible = True 'Unhide the sheet you want to work at.
'Do some filtering stuff // copy stuff
Sheets("synthèse_FR&EN").Visible = False 'Re-hide the sheet again.
Application.ScreenUpdating = False 'Turn ON all visual updates the macro does. Macro now works and shows every step visually in Excel.
End Sub
:)
关于excel - 点击隐藏工作表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53054506/