Skip to main content

如何在 Excel 中設定密碼以保護不同使用者的個別工作表?

Author: Xiaoyang Last Modified: 2025-05-12

在 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」視窗,從左側的「專案-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. 最後,當將這個活頁簿發送給其他使用者時,您應該將使用者名稱和密碼發送給該使用者。他們只能打開和編輯他們自己的工作表,並且沒有權限查看其他工作表。

最佳辦公效率工具

🤖 Kutools AI 助手:基於智能執行方式革新數據分析:智能執行   |  生成代碼  |  創建自訂公式  |  分析數據並生成圖表  |  調用 Kutools 函數
熱門功能查找、標記重複值或識別重複項   |  刪除空行   |  合併列或單元格而不丟失數據   |   四捨五入無需公式 ...
高級 LOOKUP多條件 VLookup    多值 VLookup  |   多表查找   |   模糊查找 ....
高級下拉列表快速創建下拉列表   |  依賴下拉列表   |  多選下拉列表 ....
列管理器添加特定數量的列  |  移動列  |  切換隱藏列的可見狀態  |  比較區域和列 ...
特色功能網格聚焦   |  設計檢視   |   增強編輯欄    工作簿與工作表管理器   |  資源庫(自動文本)   |  日期提取器   |  合併資料   |  加密/解密儲存格    按列表發送電子郵件   |  超級篩選   |   特殊篩選(篩選粗體/斜體/刪除線...) ...
頂級 15 種工具集12 個文本工具添加文本刪除特定字符、...)   |   50+ 圖表 類型甘特圖、...)   |   40+ 實用 公式基於生日計算年齡、...)   |   19 個插入工具插入QR碼根據路徑插入圖片、...)   |   12 個轉換工具金額轉大寫匯率轉換、...)   |   7 個合併與分割工具高級合併行分割儲存格、...)   |   ... 還有更多

使用 Kutools for Excel 提升您的 Excel 技巧,體驗前所未有的高效。 Kutools for Excel 提供超過 300 種高級功能來提高生產力並節省時間。  點擊這裡獲取您最需要的功能...


Office Tab 將標籤式界面帶到 Office,讓您的工作更加輕鬆

  • 在 Word、Excel、PowerPoint、Publisher、Access、Visio 和 Project 中啟用標籤式編輯和閱讀。
  • 在同一窗口的新標籤中打開和創建多個文檔,而不是在新窗口中。
  • 將您的生產力提高 50%,每天為您減少數百次鼠標點擊!