关闭工作表之前的消息框,以显示所有未受保护的工作表的名称

时间:2015-12-21 14:27:43

标签: excel vba excel-vba access-vba

到目前为止,我已完成了2个独立的编程。

在关闭工作簿之前显示一个消息框:

 Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim answer As String
    Dim question As String
    question = "Display all the sheets which are Unprotected"
    answer = MsgBox(question, vbYesNo)

    If answer = vbNo Then
        MsgBox "complete everything and then close"
        Cancel = True
        Exit Sub
    Else
        ThisWorkbook.Save
    End If
End Sub

另一张显示在新单张“未受保护”中,列出所有未受保护的单张。

Sub UnprotectSheet()
Dim ws As Worksheet, a As Range
ActiveWorkbook.Worksheets.Add.Name = "Unprotected"
For Each ws In ActiveWorkbook.Worksheets
 If ws.ProtectContents = False And ws.Name <> "Unprotected" Then
CNT = Sheets("Unprotected").Cells(Sheets("Unprotected").Rows.Count, "A").End(xlUp).Row
Sheets("Unprotected").Cells(CNT + 1, "A") = ws.Name
End If
Next
End Sub

如果我尝试关闭工作表,并且如果任何工作表未受保护,我希望显示一个消息框,消息框将显示未受保护的工作表的名称。我在结合上述两个代码时面临问题。 我不是VBA专家,我正在尝试但无法解决它。

2 个答案:

答案 0 :(得分:1)

这样的事情可以显示未受保护的表单列表。但是,最好只使用VBA来强制保护,而不是提示用户这样做(除非他们需要提供保护状态的密码)。

 Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim answer As String
    Dim question As String
    Dim unprotected as String
    unprotected = GetUnprotectedSheets(ThisWorkbook)
    If unprotected <> vbNullString Then 
       MsgBox "Please protected the following worksheets before closing" & vbCRLF & unprotected
       Cancel = True
       Exit Sub
    Else
        ThisWorkbook.Save
    End If
End Sub

Function GetUnprotectedSheets(wb as Workbook) 
'Custom function to return a string of sheet names
' which are unprotected
Dim ret as String
Dim ws as Worksheet
For each ws in wb.Worksheets
    If Not ws.ProtectContents Then
        ret = IIF(ret = "", ws.Name, ret & vbCRLF & ws.Name)
    End If
Next

GetUnprotectedSheets = ret

End Function

您可以调用这样的程序以确保所有工作表都受到保护:

Sub ProtectAllSheets(wb as Workbook)
Dim ws as Worksheet
For each ws in wb.Worksheets
    If Not ws.ProtectContents Then ws.Protect
Next
End Sub

答案 1 :(得分:0)

只需在第二个脚本中添加一个计数器:

Sub UnprotectSheet()
Dim ws As Worksheet, a As Range
Dim iCounter As Integer, strMessage As String   'Adding a counter variable & string
'ActiveWorkbook.Worksheets.Add.Name = "Unprotected"
iCounter = 0  'Initialize it
strMessage = "" 'Initialize empty string for the message box

For Each ws In ActiveWorkbook.Worksheets
   If ws.ProtectContents = False Then
      iCounter = iCounter + 1    'Keeping track of any unprotected sheet
'      CNT = Sheets("Unprotected").Cells(Sheets("Unprotected").Rows.Count, "A").End(xlUp).Row
'      Sheets("Unprotected").Cells(CNT + 1, "A") = ws.Name
      strMessage = strMessage & ws.Name & " "
   End If
Next
' Here you can do your msgbox or any other action if unprotected sheet detected


If iCounter > 0 Then
   MsgBox ("These sheets are unprotected: " & strMessage)
End If

End Sub

编辑:

要将其包含在按钮内,请单击:向表单添加activeX按钮,然后:

Private Sub CommandButton1_Click()
'E.g. make the sub a commmandbutton_click() event

End Sub

实际上,当您在表单中添加按钮时,如果右键单击它,则可以选择&#34;查看代码&#34; - 这将创建一个关联的Commandbutton_click,如上所示。