跳至主要内容

如何在 Excel 中为不同的用户设置密码以保护各个工作表?

Author: Xiaoyang Last Modified: 2025-07-31

在 Excel 中,您可以为不同的工作表设置不同的密码。这意味着一个用户可以使用一个密码对一个工作表进行更改,而另一个用户可以使用不同的密码对另一个工作表进行更改。但是,有时您可能希望每个用户只能查看和访问他们自己的工作表。这在 Excel 中是否可以实现呢?


为了单独保护每个工作表并限制用户对工作表的访问,请应用以下 VBA 代码:

1. 打开一个新的工作簿,并创建一个名为“主工作表”的新工作表,见截图:

A screenshot of the Main sheet in Excel where the first worksheet is created

2. 然后,按“Alt”+“F11”键打开“Microsoft Visual Basic for Applications”窗口,双击左侧“Project-VBAProject”窗格中的“ThisWorkbook”以打开一个空白代码模块,然后将以下 VBA 代码复制并粘贴到代码窗口中,见截图:

VBA 代码:设置密码以保护用户的单个工作表

Option Explicit
Dim gUserName As String
Dim gUserPass As String
Private Sub Workbook_Open()
'Updateby ExtendOffice
Dim xWShs As Sheets
Dim xWSh As Worksheet
Dim xUserName As String
Dim xPass As String
Dim xBolH As Boolean
GTINPUT:
xUserName = InputBox("Enter the user name")
If TypeName(xUserName) = "String" Then
    If xUserName = "" Then
        Exit Sub
    End If
End If
xUserName = LCase(xUserName)
xPass = InputBox("User name:" & xUserName & Chr(13) & Chr(10) & "Enter the password:")
If TypeName(xPass) = "String" Then
    If xPass = "" Then
    MsgBox "The password is incorrect, please enter the user name and password again."
    GoTo GTINPUT
    End If
Else
    MsgBox "The password is incorrect, please enter the user name and password again."
    GoTo GTINPUT
End If
Set xWShs = Worksheets
xBolH = False
For Each xWSh In Worksheets
    If xWSh.Name = xUserName Then
    xBolH = True
    Exit For
    End If
Next
If xBolH Then
Set xWSh = xWShs(xUserName)
On Error GoTo GTINPUT2
xWSh.Unprotect (xPass)
xWSh.Visible = True
xWSh.Activate
Else
Set xWSh = xWShs.Add
xWSh.Name = xUserName
xWSh.Activate
End If
gUserName = xUserName
gUserPass = xPass
Exit Sub
GTINPUT2:
    MsgBox "The password is incorrect, please enter the user name and password again."
    GoTo GTINPUT
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim xWSh As Worksheet
On Error Resume Next
Set xWSh = Worksheets(gUserName)
xWSh.Protect Password:=gUserPass, DrawingObjects:=False, Contents:=True, Scenarios:= _
False, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
AllowFormattingRows:=True, AllowInsertingColumns:=True, AllowInsertingRows _
:=True, AllowInsertingHyperlinks:=True, AllowDeletingColumns:=True, _
AllowDeletingRows:=True, AllowSorting:=True, AllowFiltering:=True, _
AllowUsingPivotTables:=True
For Each xWSh In Worksheets
    If xWSh.Name <> "Main" Then
        xWSh.Visible = xlSheetVeryHidden
    End If
Next xWSh
ActiveWorkbook.Save
End Sub

A screenshot of the Visual Basic for Applications window with the VBA code to set passwords for individual sheets

3. 接着,将光标放在“Private Sub Workbook_Open()”脚本的末尾,按“F5”键运行此代码。现在,在弹出的提示框中,输入要为此用户创建工作表的用户名,见截图:

A screenshot of the prompt box asking for a user name to create a new sheet in Excel

4. 然后,点击“确定”按钮,在接下来的提示框中,输入用于保护该工作表的密码,见截图:

A screenshot of the prompt box asking for a password to protect the newly created sheet in Excel

5. 继续点击“确定”按钮,随即会创建一个以用户名命名的新工作表,您可以根据需要为该用户创建数据。见截图:

A screenshot of the newly created sheet named after the user in Excel

6. 重复上述步骤 3 至步骤 5,逐一创建其他所需的工作表。

7. 创建完工作表后,通过点击“文件”>“另存为”保存当前工作簿。在“另存为”对话框中,指定文件名,然后从“指定保存格式”下拉列表中选择“Excel 启用宏的工作簿 (*.xlsm)”格式,见截图:

A screenshot of the Save As dialog box in Excel, showing the Save as type dropdown list for Excel Macro-Enabled Workbook (*.xlsm)

8. 然后点击保存按钮保存该文件。

9. 接着关闭工作簿并重新打开它,然后点击编辑栏顶部的“启用内容”以激活代码。

A screenshot of the Enable Content button in Excel to activate the VBA code

10. 现在,会弹出一个提示框,提醒您输入用户名和密码,以便特定用户打开相应的工作表。

11. 最后,当将此工作簿发送给其他用户时,您应将用户名和密码发送给相应的用户。他们只能打开和编辑自己的工作表,无权查看其他工作表。

最佳 Office 办公效率工具

🤖 Kutools AI 助手:基于智能执行,彻底革新数据分析 生成代码创建自定义公式分析数据并生成图表调用 Kutools Functions
热门功能:查找、选中项的背景色或标记重复项|删除空行|合并列或单元格且不丢失数据|四舍五入(无公式)...
高级 LOOKUP多条件查找 (VLookup)多值查找 (VLookup)多表查找 (VLookup Across Multiple Sheets)模糊查找 (Fuzzy Lookup)...
高级下拉列表快速创建下拉列表依赖型下拉列表多选下拉列表...
列管理器添加指定数量的列移动列切换隐藏列的可见状态比较区域及列...
特色功能网格聚焦设计视图增强编辑栏工作簿 & 工作表管理器资源库(自动文本)|日期提取合并数据加密/解密单元格按列表发送电子邮件超级筛选特殊筛选(筛选粗体/倾斜/删除线等)...
热门15 大工具集12 款文本工具添加文本删除特定字符等)50+ 种图表 类型甘特图等)40+ 实用公式基于生日计算年龄等)19 款插入工具插入二维码按路径插入图片等)12 种转换工具小写金额转大写汇率转换等)7 款合并与分割工具高级合并行分割单元格等)...更多精彩等你发现
使用 Kutools,支持您的首选语言——包括英语、西班牙语、德语、法语、中文及40 多种其他语言!

用 Kutools for Excel 加速你的 Excel 技能,体验前所未有的高效办公。 Kutools for Excel 提供300 多项高级功能,助您提升效率,节省大量时间。点击此处,获取你最需要的功能...


Office Tab 为 Office 带来标签式界面,让你的工作更加轻松

  • 在 Word、Excel、PowerPoint 启用标签式编辑和阅读
  • 在同一窗口的新标签中打开和创建多个文档,无需新建窗口。
  • 办公效率提升50%,每天帮你减少上百次鼠标点击!