有朋友问:如果用户工作簿禁用了宏,怎样对特定工作表进行隐藏,让用户无法查看?只有当宏被打开时显示其他工作表?
宏被禁用提示
下表是当用户禁用工作簿宏时,隐藏其他工作表的同时显示提示:
提示表提示界面
要实现这样的功能,我们可以通过实现Workbook_Open、Workbook_BeforeClose、Workbook_BeforeSave等事件处理程序,我们可以控制工作簿在打开、关闭和保存时的行为。例如,在打开工作簿时自动显示所有工作表,并在关闭前检查是否需要保存更改;通过拦截标准的保存操作,我们可以调用自定义的保存函数CustomSave,该函数允许用户选择保存位置、格式,并在保存前隐藏所有非警告工作表,以避免不必要的干扰或数据泄露。同时,将介绍如何编写HideAllSheets和ShowAllSheets子程序来智能管理工作表的可见性。这些子程序将帮助用户隐藏除警告工作表外的所有工作表,并在需要时重新显示它们,从而保护敏感数据或简化工作簿的视图。代码片段如下:
' 强制显式声明变量
Option Explicit
' 将警告工作表的名称分配给一个常量
Const Warning As String = "提示"
' 工作簿打开时自动执行的宏
Private Sub Workbook_Open()
' 关闭屏幕更新
Application.ScreenUpdating = False
' 调用显示所有工作表的例程
Call ShowAllSheets
' 将工作簿的已保存属性设置为True
Me.Saved = True
' 打开屏幕更新
Application.ScreenUpdating = True
End Sub
' 工作簿关闭前执行的宏
Private Sub Workbook_BeforeClose(Cancel As Boolean)
' 声明变量
Dim Ans As Integer
' 如果工作簿的已保存属性为False,则模拟Excel的默认保存提示
If Me.Saved = False Then
Do
Ans = MsgBox("你想保存对 '" & Me.Name & "' 的更改吗?", vbQuestion vbYesNoCancel)
Select Case Ans
Case vbYes
' 调用自定义保存例程
Call CustomSave
Case vbNo
Me.Saved = True
Case vbCancel
Cancel = True
Exit Sub
End Select
Loop Until Me.Saved
End If
End Sub
' 工作簿保存前执行的宏
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
' 取消常规保存
Cancel = True
' 调用自定义保存例程
Call CustomSave(SaveAsUI)
End Sub
' 自定义保存例程
Private Sub CustomSave(Optional SaveAs As Boolean)
' 声明变量
Dim ActiveSht As Object
Dim FileFormat As Variant
Dim FileName As String
Dim FileFilter As String
Dim FilterIndex As Integer
Dim Msg As String
Dim Ans As Integer
Dim OrigSaved As Boolean
Dim WorkbookSaved As Boolean
' 关闭屏幕更新
Application.ScreenUpdating = False
' 关闭事件,防止在保存时再次触发BeforeSave事件
Application.EnableEvents = False
' 将工作簿的已保存属性保存到变量
OrigSaved = Me.Saved
' 获取当前活动的工作表
Set ActiveSht = ActiveSheet
' 调用隐藏所有工作表(除了提示工作表)的例程
Call HideAllSheets
' 根据是否需要另存为或路径为空,处理保存逻辑
' ...(此处省略了部分逻辑以保持注释简洁)
'...
' 调用显示所有工作表的例程
Call ShowAllSheets
' 激活之前的活动工作表
ActiveSht.Activate
' 根据是否成功保存,更新工作簿的已保存属性
If WorkbookSaved Then
Me.Saved = True
Else
Me.Saved = OrigSaved
End If
' 打开屏幕更新
Application.ScreenUpdating = True
' 打开事件
Application.EnableEvents = True
End Sub
Private Sub HideAllSheets()
Dim Sh As Object
Sheets("提示").Visible = xlSheetVisible
For Each Sh In Sheets
If Sh.Name <> Warning Then
Sh.Visible = xlSheetVeryHidden
End If
Next Sh
End Sub
Private Sub ShowAllSheets()
Dim Sh As Object
For Each Sh In Sheets
If Sh.Name <> Warning Then
Sh.Visible = xlSheetVisible
End If
Next Sh
Sheets("提示").Visible = xlSheetVeryHidden
End Sub
Private Function IsLegalFilename(ByVal fname As String) As Boolean
Dim BadChars As Variant
Dim i As Long
If Len(fname) > 218 Then
IsLegalFilename = False
Exit Function
Else
BadChars = Array("\", "/", "<", ">", "?", "[", "]", ":", "|", "*", """")
fname = GetFileName(fname)
For i = LBound(BadChars) To UBound(BadChars)
If InStr(1, fname, BadChars(i)) > 0 Then
IsLegalFilename = False
Exit Function
End If
Next i
End If
IsLegalFilename = True
End Function
Private Function GetFileName(ByVal FullName As String) As String
Dim i As Long
For i = Len(FullName) To 1 Step -1
If Mid(FullName, i, 1) = Application.PathSeparator Then Exit For
Next i
GetFileName = Mid(FullName, i 1)
End Function
通过本篇文章,读者将学习到如何通过VBA编程来扩展Excel的功能,使其更加符合个人或团队的特定需求。