VBA解除Excel工作表保护

Excel工作表保护解除

工作表保护后无法编辑内容,可能是密码忘记,不可暴力破解隐私

1 打开需的Excel

2 Alt + F11 打开代码编辑,点击任意代码编辑项,将如下代码复制,并运行。

Public Sub GetWorkbookPassword()

Dim w1 As Worksheet, w2 As Worksheet
Dim i As Integer, j As Integer, k As Integer, l As Integer
Dim m As Integer, n As Integer, i1 As Integer, i2 As Integer
Dim i3 As Integer, i4 As Integer, i5 As Integer, i6 As Integer
Dim PWord1 As String
Dim ShTag As Boolean, WinTag As Boolean

Application.ScreenUpdating = False
Application.DisplayAlerts = False


    WinTag = ActiveWorkbook.ProtectStructure Or ActiveWorkbook.ProtectWindows

    ShTag = False
    For Each w1 In Worksheets
        ShTag = ShTag Or w1.ProtectContents
    Next w1

    If Not ShTag And Not WinTag Then
        MsgBox "该工作表没有设置保护密码", , "系统提示"
        Exit Sub
    End If

    If Not WinTag Then
    Else
        On Error Resume Next
        Do
        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
            ActiveWorkbook.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 ActiveWorkbook.ProtectStructure = False And ActiveWorkbook.ProtectWindows = False Then
                PWord1 = Chr(i) & Chr(j) & Chr(k) & Chr(l) & Chr(m) & Chr(i1) & Chr(i2) & Chr(i3) & Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)
                Exit Do
            End If

        Next: Next: Next: Next: Next: Next
        Next: Next: Next: Next: Next: Next
        Loop Until True
        On Error GoTo 0
    End If

    If WinTag And Not ShTag Then
        MsgBox "确保密码唯一的?", , "系统提示"
        Exit Sub
    End If

    On Error Resume Next
    For Each w1 In Worksheets
        w1.Unprotect PWord1
    Next w1

    On Error GoTo 0
    ShTag = False

    For Each w1 In Worksheets
        ShTag = ShTag Or w1.ProtectContents
    Next w1

    If ShTag Then
        For Each w1 In Worksheets

            If w1.ProtectContents Then
                On Error Resume Next
                Do
                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
                    w1.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 Not w1.ProtectContents Then
                        PWord1 = Chr(i) & Chr(j) & Chr(k) & Chr(l) & Chr(m) & Chr(i1) & Chr(i2) & Chr(i3) & Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)
                        For Each w2 In Worksheets
                            w2.Unprotect PWord1
                        Next w2
                        Exit Do
                    End If
                Next: Next: Next: Next: Next: Next
                Next: Next: Next: Next: Next: Next
                Loop Until True
                On Error GoTo 0
            End If
        Next w1
    End If
    MsgBox "该工作簿中的工作表密码保护已全部解除!请记得另存", , "系统提示"

Application.ScreenUpdating = False
Application.DisplayAlerts = False

End Sub
相关推荐
JavaNice哥5 小时前
easyexcel读取写入excel easyexceldemo
excel
Johaden5 小时前
EXCEL+Python搞定数据处理(第一部分:Python入门-第2章:开发环境)
开发语言·vscode·python·conda·excel
进击的雷神8 小时前
Excel 实现文本拼接方法
excel
东京老树根8 小时前
Excel 技巧15 - 在Excel中抠图头像,换背景色(★★)
笔记·学习·excel
规划GIS会10 小时前
CC工具箱使用指南:【Excel点集转面要素(批量)】
excel·二次开发·arcgis pro
东京老树根11 小时前
Excel 技巧17 - 如何计算倒计时,并添加该倒计时的数据条(★)
笔记·学习·excel
符小易12 小时前
Mac苹果电脑 怎么用word文档和Excel表格?
macos·word·excel
一名技术极客1 天前
Python 进阶 - Excel 基本操作
android·python·excel
qq_12039813371 天前
EXCEL的一些用法记录
excel
hakesashou2 天前
python如何导出数据到excel文件
开发语言·python·excel