我在网上遇到了一些迫使用户启用宏的vba代码。当宏关闭时它只显示“Sheet1”,当它们打开时,它会隐藏“Sheet1”并取消隐藏工作表。这是一个非常巧妙的代码,但有一个缺点。当我关闭文件时它不会问我是否要保存文件。它会自动保存,以便隐藏其余的纸张。我想知道是否有办法解决这个问题,即如果我可以启用“保存/关闭而不保存”提示,并且除了“Sheet1”之外还保持工作表隐藏
以下是代码:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
'Step 1: Declare your variables
Dim ws As Worksheet
'Step 2: Unhide the Starting Sheet
Sheets("Sheet1").Visible = xlSheetVisible
'Step 3: Start looping through all worksheets
For Each ws In ThisWorkbook.Worksheets
'Step 4: Check each worksheet name
If ws.Name <> "Sheet1" Then
'Step 5: Hide the sheet
ws.Visible = xlVeryHidden
End If
'Step 6: Loop to next worksheet
Next ws
'Step 7: Save the workbook
ActiveWorkbook.Save
End Sub
Private Sub Workbook_Open()
'Step 1: Declare your variables
Dim ws As Worksheet
'Step 2: Start looping through all worksheets
For Each ws In ThisWorkbook.Worksheets
'Step 3: Unhide All Worksheets
ws.Visible = xlSheetVisible
'Step 4: Loop to next worksheet
Next ws
'Step 5: Hide the Start Sheet
Sheets("Sheet1").Visible = xlVeryHidden
End Sub
答案 0 :(得分:1)
您可以使用自己的消息包装ActiveWorkbook.Save
,如下所示:
If MsgBox("Would you like to save?", vbYesNo + vbQuestion, "Save Workbook") = vbYes Then
ActiveWorkbook.Save
End If