【发布时间】:2019-05-27 05:21:15
【问题描述】:
我正在尝试为 Excel 工作簿中的多个工作表提供一种次要形式的访问保护。我知道这并不容易实现,并且仍然存在保护问题。
我在下面有关于如何实现这一目标的工作代码。但是,它仅适用于工作簿中的一张工作表。有没有办法在这段代码中添加多个工作表。
注意:我不想创建同一个工作簿的多个版本。我只想要一个简单的密码来访问工作表。我了解这并不提供万无一失的方法或限制访问
Private Sub Workbook_Open()
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim MySheetName As String
MySheetName = "Sheet1" 'The first sheet which I want to hide.
MySheetName = "Sheet2" 'The second sheet which I want to hide.
If Application.ActiveSheet.Name = MySheetName Then
Application.EnableEvents = False
Application.ActiveSheet.Visible = False
response = Application.InputBox("Password", "Enter Password", "", Type:=2)
If response = "1234" Then 'Unhide Password.
Application.Sheets(MySheetName).Visible = True
Application.Sheets(MySheetName).Select
End If
End If
Application.Sheets(MySheetName).Visible = True
Application.EnableEvents = True
End Sub
此代码仅适用于单个工作表。它可以适应在多张纸上提供保护吗?
【问题讨论】:
-
您分配
MySheetName = "Sheet1"然后立即用MySheetName = "Sheet2"覆盖它。这就是为什么它只适用于 sheet2。此外,您有一个事件参数Sh,它指的是刚刚激活的工作表,使用它而不是ActiveSheet。第三,当您隐藏活动工作表时,另一个工作表将变为活动状态。那可能是您要保护的另一张纸。