前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >VBA: 为worksheet 设置密码和解除密码

VBA: 为worksheet 设置密码和解除密码

作者头像
Exploring
发布2023-08-17 08:13:13
1.4K0
发布2023-08-17 08:13:13
举报

文章背景: 在工作中,有时候需要给工作表的的内容设置保护,避免数据被误修改,同时又希望可以通过宏命令,实现数据处理的自动化。此时,我们可以在宏命令中添加相应的代码:在程序执行前,解除密码;在程序结束后,设置密码。

1 判断工作表是否处于保护状态

ProtectContents是工作表的属性,用于判断工作簿中的某张表是否处于保护状态。

语法:expression.ProtectContents

  • expression: A variable that represents a Worksheet object.
  • True if the contents of the sheet are protected.

代码示例:

代码语言:javascript
复制
If Worksheets("Sheet1").ProtectContents = True Then 
 MsgBox "The contents of Sheet1 are protected." 
End If
2 保护和解除保护工作表
2.1 保护工作表

在VBA中可以使用Worksheet对象的Protect方法保护工作表。

语法:expression.Protect (Password, DrawingObjects, Contents, Scenarios, UserInterfaceOnly, AllowFormattingCells, AllowFormattingColumns, AllowFormattingRows, AllowInsertingColumns, AllowInsertingRows, AllowInsertingHyperlinks, AllowDeletingColumns, AllowDeletingRows, AllowSorting, AllowFiltering, AllowUsingPivotTables)

  • expression: A variable that represents a Worksheet object.
  • Protects a worksheet so that it cannot be modified.
  • 参数的具体使用见文末的参考资料[5]。

Protect方法的所有参数都是可选的;Password参数可以不提供,表示没有设置密码保护excel工作表。其余的部分参数对应的是“保护工作表”对话框中显示的“允许此工作表的所有用户进行的选项”,如下图所示:

值得一提的是,在保护工作表之前,需要对受保护的单元格区域设置锁定。这样,在保护工作表期间,那些锁定单元格得到保护,其他未锁定的单元格依然可以编辑。

2.2 解除保护工作表

在VBA中可以使用Worksheet对象的Unprotect方法解除保护工作表。

语法:expression.Unprotect (Password)

  • expression: A variable that represents a Worksheet object.
  • Removes protection from a sheet or workbook. This method has no effect if the sheet or workbook isn't protected.
3 综合示例

假设有张工作表Sheet1,密码是“111”,单元格区域已锁定。在数据处理前,解除密码保护;数据处理结束之后,再设置密码保护。

代码语言:javascript
复制
Option Explicit

Sub EditData()

    '修改数据
    Dim sht As Worksheet, flag_protect As Boolean
    
    Set sht = ThisWorkbook.Worksheets("Sheet1")
    flag_protect = False
    
    '1 取消密码保护
    If sht.ProtectContents = True Then
    
        sht.Unprotect Password:="111"  '撤消工作表保护并取消密码
        
        flag_protect = True
         
    End If
    
    '2 修改数据
    sht.Range("B3").Value2 = "30"
    

    '3 保护工作表并设置密码
    If flag_protect Then
    
        sht.Protect Password:="111", Contents:=True, _
        AllowFormattingCells:=True, AllowFormattingColumns:=True, _
        AllowFormattingRows:=True
    
    End If
    
    MsgBox "Done!"
    
End Sub

参考资料:

[1] vba 之判断工作表是否处于保护状态:Worksheets.ProtectContents(https://blog.csdn.net/ljr_123/article/details/106946830)

[2] Worksheet.ProtectContents property (Excel)(https://learn.microsoft.com/en-us/office/vba/api/excel.worksheet.protectcontents)

[3] VBA为worksheet 设置密码和解除密码(https://blog.csdn.net/weixin_44741335/article/details/105566561)

[4] 如何用vba批量保护或取消保护excel工作表(http://www.exceloffice.net/archives/1293)

[5] Worksheet.Protect method(https://learn.microsoft.com/en-us/office/vba/api/excel.worksheet.protect)

[6] Worksheet.Unprotect method(https://learn.microsoft.com/en-us/office/vba/api/excel.worksheet.unprotect)

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2023-05-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 数据处理与编程实践 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1 判断工作表是否处于保护状态
  • 2 保护和解除保护工作表
    • 2.1 保护工作表
      • 2.2 解除保护工作表
      • 3 综合示例
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档