当前位置: 首页 > article >正文

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


http://www.kler.cn/a/326832.html

相关文章:

  • 初学stm32 --- NVIC中断
  • 【java基础系列】实现数字的首位交换算法
  • Redis分布式锁释放锁是否必须用lua脚本?
  • 路由器的原理
  • VR线上展厅的色彩管理如何影响用户情绪?
  • myexcel的使用
  • 浅析Android中的View事件分发机制
  • 19.2 编写dockerfile和k8s yaml
  • 迁移学习案例-python代码
  • Redis一些简单通用命令认识常用数据类型和编码方式认识Redis单线程模型
  • OJ题之单链表排序
  • 智慧城市运营模式--联合公司运营
  • ThinkPHP发送邮件教程:从配置到发送指南!
  • ChatGPT的150个角色提示场景实测(9)讲故事
  • django drf 分页器
  • 【Spring基础3】- Spring的入门程序
  • 【从0开始搭建微服务并进行部署】SpringBoot+dubbo+zookeeper
  • 数据结构——栈的基本操作
  • ELK-02-skywalking-v10.0.1安装
  • 为什么要自定义异常
  • 几个可以给pdf加密的方法,pdf加密详细教程。
  • AI新方向:OpenAI o1是一个更擅长思考的模型系列:高级推理+逻辑严密+更广泛的知识,用于解决复杂的逻辑问题,慢思考
  • Android开发中的ViewModel
  • Unity3D Compute Shader同步详解
  • 刷题训练之队列与宽搜
  • d3.js 基础学习