excelperfect 在《VBA代码库10:强制用户启用宏》中,讲解了一段用户在打开工作簿时必须启用宏才能使用工作簿功能的代码。本文给出另一段同样可以实现强制用户启用宏的代码。 如果用户没有启用宏,那么当打开工作簿时,一个特定的工作表将提示用户启用宏,而工作簿中的其他工作表则被隐藏。 代码如下: Private Sub Workbook_Open() With Application '禁用ESC键 .EnableCancelKey = xlDisabled .ScreenUpdating = False Call UnhideSheets .ScreenUpdating = True '重新启用ESC键 .EnableCancelKey = xlInterrupt End With End Sub Private Sub UnhideSheets() Dim Sheet As Object For Each Sheet In Sheets If Not Sheet.Name = '提示' Then Sheet.Visible = xlSheetVisible End If Next Sheets('提示').Visible = xlSheetVeryHidden 'Application.Goto Worksheets(1).[A1], True '< 可选的 Set Sheet = Nothing ActiveWorkbook.Saved = True End Sub Private Sub Workbook_BeforeClose(Cancel As Boolean) With Application .EnableCancelKey = xlDisabled .ScreenUpdating = False Call HideSheets .ScreenUpdating = True .EnableCancelKey = xlInterrupt End With End Sub Private Sub HideSheets() Dim Sheet As Object '< 包括工作表和图表工作表 With Sheets('提示') '工作表的隐藏构成了一种变化,这种变化产生了 '自动的'保存?'提示,所以如果工作簿已经 '在此之前被保存,那么下一行和前几行 '与下面的.[A100]有关,绕过“保存?”对话... If ThisWorkbook.Saved = True Then.[A100] = 'Saved' .Visible = xlSheetVisible For Each Sheet In Sheets If Not Sheet.Name = '提示' Then Sheet.Visible =xlSheetVeryHidden End If Next If .[A100] = 'Saved' Then .[A100].ClearContents ThisWorkbook.Save End If Set Sheet = Nothing End With End Sub 注:本文的代码整理自vbaexpress.com。 |
|
来自: hercules028 > 《VBA》