【发布时间】:2017-05-29 12:52:44
【问题描述】:
您好,我有一个按钮,可以让我的老板根据他输入的工作表名称来删除工作簿中众多工作表中的其中一个工作表。此删除工作表按钮受密码保护,因为其他人使用工作簿我不希望他们删除任何内容。
现在这不会阻止他们右键单击特定工作表并删除,因此我需要一种方法来在未按下“删除工作表”按钮时保护所有工作表,并在正确输入该按钮的密码后取消保护所有工作表, 因为按钮无法移除受保护的工作表。
删除工作表按钮代码:
Private Sub CommandButton4_Click()
Dim delSheet As String
Dim response As String
Dim SheetFound As Boolean
Dim MyPass As String
Dim MyPasswrd As String, answ As String
MyPasswrd = "test" 'password verification puts trigger in cell A100, an deletes when file close
If Range("A101").Value <> "OK" Then
answ = InputBox("Please Enter The Password To Continue.", "Enter Password")
If answ <> MyPasswrd Then
MsgBox "Incorrect Password!", vbExclamation, "Warning"
Exit Sub
End If
Range("A101").Value = "OK"
End If
delSheet = InputBox("Please Enter The LAST NAME Of The DTS You Want To Remove", "Remove A DTS") 'user input
If delSheet = "" Then
MsgBox "You Did Not Complete The Entry.", vbOKOnly + vbInformation, "Warning" 'if NULL input displays this message
Exit Sub
Else
If IsLetter(delSheet) = False Then GoTo Display 'checks the user input
response = MsgBox("WARNING!! This Action Cannot Be Undone, Do You Still Want To Continue?", vbExclamation + vbYesNo, "Warning") 'verfies user input
If response = vbYes Then 'if input is yes selects sheet IF ITS FOUND
On Error Resume Next
ActiveWorkbook.Sheets(delSheet).Select
If Err = 0 Then SheetFound = True 'searches for sheet
On Error GoTo 0
If SheetFound = False Then 'if sheet not found displays this message
MsgBox prompt:="The sheet '" & delSheet & "' Could Not Be Found In This File!", Buttons:=vbExclamation, Title:="Search Result"
Exit Sub
Else
Application.DisplayAlerts = False 'Finally deletes sheet and bypass xcel warning for sheet deletion
Sheets(delSheet).Delete
Application.DisplayAlerts = True
MsgBox ("The DTS " & delSheet & " Was Successfully Removed") 'message for sucessfully deleting the sheet
Application.Goto Reference:=Worksheets("Control Center").Range("B1"), Scroll:=True
End If
Else
response = vbNo 'if user does not want to delete sheet exits window
Exit Sub
Display:
MsgBox "Invalid Character In Last Name. Please Only Use Letters And Numbers(1-9), NOT Spaces and Specail Characters (! @ # $ % ^ & * - + = \ _ .)", vbExclamation, "Warning"
End If
End If
End Sub
【问题讨论】:
-
表格删除不能停止,至少在没有大量代码/结构保护的情况下不能。但是在保存工作簿之前,您始终可以确定工作表是否存在,如果它不存在,那么您可以阻止保存工作簿。这是最简单最简单的路线。
-
我做了一些研究,发现了这个Delete A protected sheet,但我不知道如何在我的代码中实现它。基本上,您创建一个包含所有工作表名称的控制工作表,然后当您单击控件中的工作表时,它会运行一个宏来取消保护所有工作表以便将其删除,然后工作簿中的工作表将恢复为受保护状态。