用VBA去除Excel工作表保护密码

今天帮同事解决个EXCEL问题,现记在这,以作备忘

现象: 想要修改保护单元 格的内容,在使用“工具”菜单“保护”子菜单的“撤消工作表保护”命令,这时要求输入密码

解决方案:网上找到一文http://www.excel123.cn/Article/excelvba/200812/126.html

1.按Alt+F11,打开VBA编辑器。

    2.在“工程”窗口中选择要取消保护的工作表名称,单击菜单“插入→模块”。

    3.在右侧的代码窗口中输入下列代码:

Sub Remove_WorkSheet_Password()
   Dim i As Integer, j As Integer, k As Integer
   Dim l As Integer, m As Integer, n As Integer
   Dim i1 As Integer, i2 As Integer, i3 As Integer
   Dim i4 As Integer, i5 As Integer, i6 As Integer
   On Error Resume Next
   If ActiveSheet.ProtectContents = False Then
       MsgBox "该工作表没有保护密码!"
       Exit Sub
   End If
   For i = 65 To 66: For j = 65 To 66: For k = 65 To 66
   For l = 65 To 66: For m = 65 To 66: For i1 = 65 To 66
   For i2 = 65 To 66: For i3 = 65 To 66: For i4 = 65 To 66
   For i5 = 65 To 66: For i6 = 65 To 66: For n = 32 To 126

ActiveSheet.Unprotect Chr(i) & Chr(j) & Chr(k) & _
         Chr(l) & Chr(m) & Chr(i1) & Chr(i2) & Chr(i3) & _
         Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)
   If ActiveSheet.ProtectContents = False Then
         MsgBox "已经解除了工作表保护!"
         Exit Sub
   End If
   Next: Next: Next: Next: Next: Next
   Next: Next: Next: Next: Next: Next
End Sub


   4.关闭VBA编辑器,回到工作表中,按Alt+F8,打开“宏”对话框,选择“Remove_WorkSheet_Password”,并单击“执行”按钮。稍等片刻工作表保护将被取消。

   

转载于:https://www.cnblogs.com/yuanbao/archive/2009/05/31/1492961.html

你可能感兴趣的:(用VBA去除Excel工作表保护密码)