Note: The other languages of the website are Google-translated. Back to English
登入  \/ 
x
or
x
註冊  \/ 
x

or

如何在不保護Excel中整個工作表的情況下鎖定指定的單元格?

通常,您需要保護整個工作表以防止單元格被編輯。 有什麼方法可以鎖定單元格而不保護整個工作表嗎? 本文為您推荐一種VBA方法。

鎖定指定的單元格,而不用VBA保護整個工作表


鎖定指定的單元格,而不用VBA保護整個工作表


假設您需要鎖定當前工作表中的單元格A3和A5,下面的VBA代碼將幫助您在不保護整個工作表的情況下實現它。

1.右鍵單擊工作表標籤,然後選擇 查看代碼 從右鍵單擊菜單中。

2.然後將下面的VBA代碼複製並粘貼到“代碼”窗口中。 看截圖:

VBA代碼:鎖定指定的單元格而不保護整個工作表

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Column = 1 Then
        If Target.Row = 3 Or Target.Row = 5 Then
            Beep
            Cells(Target.Row, Target.Column).Offset(0, 1).Select
        End If
    End If
End Sub

備註:在代碼中, 列1, 行= 3行= 5 表示運行代碼後,當前工作表中的單元格A3和A5將被鎖定。 您可以根據需要更改它們。

3。 按 其他 + Q 同時關閉按鍵 Microsoft Visual Basic for Applications 窗口。

現在,單元格A3和A5已鎖定在當前工作表中。 如果您嘗試在當前工作表中選擇單元格A3或A5,則光標將自動移動到右側相鄰的單元格。


相關文章:


最佳辦公效率工具

Kutools for Excel解決了您的大多數問題,並使您的生產率提高了80%

  • 重用: 快速插入 複雜的公式,圖表 以及您以前使用過的任何東西; 加密單元 帶密碼 創建郵件列表 並發送電子郵件...
  • 超級公式欄 (輕鬆編輯多行文本和公式); 閱讀版式 (輕鬆讀取和編輯大量單元格); 粘貼到過濾範圍...
  • 合併單元格/行/列 不會丟失數據; 拆分單元格內容; 合併重複的行/列...防止細胞重複; 比較範圍...
  • 選擇重複或唯一 行; 選擇空白行 (所有單元格都是空的); 超級查找和模糊查找 在許多工作簿中; 隨機選擇...
  • 確切的副本 多個單元格,無需更改公式參考; 自動創建參考 到多張紙; 插入項目符號,複選框等...
  • 提取文字,添加文本,按位置刪除, 刪除空間; 創建和打印分頁小計; 在單元格內容和註釋之間轉換...
  • 超級濾鏡 (將過濾方案保存並應用於其他工作表); 高級排序 按月/週/日,頻率及更多; 特殊過濾器 用粗體,斜體...
  • 結合工作簿和工作表; 根據關鍵列合併表; 將數據分割成多個工作表; 批量轉換xls,xlsx和PDF...
  • 超過300種強大功能。 支持Office / Excel 2007-2019和365。支持所有語言。 在您的企業或組織中輕鬆部署。 完整功能30天免費試用。 60天退款保證。
kte選項卡201905

Office選項卡為Office帶來了選項卡式界面,使您的工作更加輕鬆

  • 在Word,Excel,PowerPoint中啟用選項卡式編輯和閱讀,發布者,Access,Visio和Project。
  • 在同一窗口的新選項卡中而不是在新窗口中打開並創建多個文檔。
  • 每天將您的工作效率提高50%,並減少數百次鼠標單擊!
officetab底部
Say something here...
symbols left.
You are guest
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    crystal · 3 months ago
    @Aaron Hi Aaron,
    After adding the code, please save the workbook as an Excel Macro-Enabled Workbook (click File > Save As > specify a folder for the file > choose Excel Macro-Enabled Workbook from the Save as type drop-down list > Save). After that, every time when you open the file, the code works automatically.
  • To post as a guest, your comment is unpublished.
    Aaron · 3 months ago
    Is there a way for this VBA code to run automatically every time someone opens the file?
  • To post as a guest, your comment is unpublished.
    Nikki · 6 months ago
    @crystal This is a great workaround, especially in shared workbooks where turning protection on/off is unsupported. Thank you so much.
  • To post as a guest, your comment is unpublished.
    peggywong44667799@gmail.com · 10 months ago
    I have try cannot, Hide formula without protecting.
  • To post as a guest, your comment is unpublished.
    Kasey · 1 years ago
    Hi,

    I have tried to use the code for ranges you have previously posted but it isn't working - can you advise me if I should be combining the code for ranges above or below?


    Thanks
  • To post as a guest, your comment is unpublished.
    crystal · 2 years ago
    @Charlie Hi Charlie,
    Sorry can't help you with that yet. Thank you for your comment.
  • To post as a guest, your comment is unpublished.
    Charlie · 2 years ago
    Hello,

    I used the code provided to Carlos and it did exactly what I wanted it to. Is there a way to have some ranges offset within the ROW to the right of the protected range (as the "Carlos" code already does), but have other ranges offset within the COLUMN to the cell directly below the protected range? I tried entering the "Carlos" code twice and changing the offset, but I received a variety of errors.

    Thank you
  • To post as a guest, your comment is unpublished.
    Espen · 2 years ago
    Is there any function to set for example Row 2 cell 13 to 900? Or do i need to manually punch each cell name in the code?
  • To post as a guest, your comment is unpublished.
    christophe.bourquard@gmail.com · 2 years ago
    Bonjour,
    Comment faire pour verrouiller de la cellule B8 à B10000?
    D'avance merci de votre réponse.
    Christophe
  • To post as a guest, your comment is unpublished.
    crystal · 2 years ago
    @Erin Hi Erin,
    You can manually break the code by clicking the Break button in the Microsoft Visual Basic for Applications window to unlock those ranges. And run the code to activate it again. Thanks for your comment.
  • To post as a guest, your comment is unpublished.
    Erin · 2 years ago
    Dear Crystal,

    Thank you so much for this! It works perfectly.

    I have used the code in Carlos' comment to run the macro automatically as you open the file. I was wondering if there is a way of having an "Undo" button or something like it, that allows you to cancel that code and therefore, unlock those ranges that have been locked.

    I want those to be locked most of the time, but I would like to unlock them should I need to change any of the data. The only reason I am not protecting the whole sheet is because, if I do, it deactivates the ability to expand on tables. That in turn, expand on a line chart.

    Thanks so much for the help!
  • To post as a guest, your comment is unpublished.
    crystal · 3 years ago
    @carlito2002wgn Dear Carlos,
    The following VBA code can help you solving the problem. Please fill in your ranges in the fourth line of the code and press the Alt + Q keys to return to the worksheet. Then please shift to other worksheet and then go back to current sheet to activate the code. Thank you for your comment.

    Dim xRg As Range
    Private Sub Worksheet_Activate()
    If xRg Is Nothing Then
    Set xRg = Union(Range("I10:I20"), Range("K10:K20"), Range("M10:M20"), Range("O10:O20"))
    End If
    End Sub
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim I As Integer
    Dim xRgNew As Range
    Dim xRgLCell As Range
    On Error Resume Next
    Application.EnableEvents = False
    For I = 1 To xRg.Areas.Count
    Set xRgLCell = xRg.Areas.Item(I)
    Set xRgLCell = xRgLCell(xRgLCell.Count).Offset(1, 0)
    If Target.Address = xRgLCell.Address Then
    If xRgNew Is Nothing Then
    Set xRgNew = Target
    Else
    Set xRgNew = Union(xRgNew, Target)
    End If
    End If
    Next
    Set xRg = Union(xRg, xRgNew)
    Application.EnableEvents = True
    End Sub
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    On Error GoTo Exitsub
    If (Not Intersect(xRg, Target) Is Nothing) And (Target.Count = 1) Then
    Target.Offset(0, 1).Select
    End If
    Exitsub:
    End Sub
  • To post as a guest, your comment is unpublished.
    carlito2002wgn · 3 years ago
    Dear Crystal,

    Is there anyway that this macro can be run automatically upon opening the file, or upon clicking any cell. I don't want people to have to run the macro manually in order for the required ranges to be protected.

    Also, I will need to protect 12 non adjacent ranges. For example: I11:I20 and K11:K20 and M11:20 etc... How do I do this?

    Last, and this one might be asking too much, but is it possible to apply the protection to the example ranges that I provided above, BUT then extend the protection down additional rows as new ones are added? In other words, protection would be applied to I11:I20 and K11:K20 and M11:20, but a user would be able to add a new row (row 21), with new data, but once the new row is added, then the protection would apply to I11:I21 and K11:K21 and M11:21. Am I asking for the moon? :-)

    Thank you for everything that you've already provided! I can't thank you and other people like you enough for sharing the your knowledge. Amazing!
  • To post as a guest, your comment is unpublished.
    crystal · 3 years ago
    @pabloizquierdo88@gmail.com Good Day,
    As the VBA code provided below, please change the specified range "H:J,4:46" to the table range you only want to lock in the worksheet.
    And cells(1,1) should be the cell outside the table range. When clicking on any cell in the table range, the cursor will be moved to that cell automatically.

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim xRg As Range
    Dim xRgEx As Range
    Dim xRgExEach As Range
    On Error Resume Next
    Application.ScreenUpdating = False
    Set xRg = Range("H:J,4:46") 'Change the row range and column range you will lock without protecting worksheet
    Set xRgEx = Application.Intersect(xRg, Target)
    If xRgEx Is Nothing Then Exit Sub
    Cells(1, 1).Select 'Specify a cell you will shift to after selecting the locked cells
    Application.ScreenUpdating = True
    End Sub
  • To post as a guest, your comment is unpublished.
    pabloizquierdo88@gmail.com · 3 years ago
    Dear Crystal,

    You have provided me with a solution (half of it) that I have been struggling for the last few weeks, but I would need some more hints.

    How this could be applied to a table range only, not whole worksheet? Thanks in advance.
  • To post as a guest, your comment is unpublished.
    crystal · 3 years ago
    @AJ Dear AJ,
    If you want to lock a range of specified rows and columns, please try the below VBA script.

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim xRg As Range
    Dim xRgEx As Range
    Dim xRgExEach As Range
    On Error Resume Next
    Application.ScreenUpdating = False
    Set xRg = Range("H:J,4:46") 'Change the row range and column range you will lock without protecting worksheet
    Set xRgEx = Application.Intersect(xRg, Target)
    If xRgEx Is Nothing Then Exit Sub
    Cells(1, 1).Select 'Specify a cell you will shift to after selecting the locked cells
    Application.ScreenUpdating = True
    End Sub
  • To post as a guest, your comment is unpublished.
    crystal · 3 years ago
    @ajay Dear ajay,
    If you want to hide formula in cell O1 without protecting the worksheet, please try the below VBA script.
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Static TheFormula As String
    If Target.Address = "$O$1" Then
    With Target
    TheFormula = .Formula
    .Value = .Value
    End With
    Else
    With Range("O1")
    If Not .HasFormula Then
    .Formula = TheFormula
    End If
    End With
    End If
    End Sub

    After using the code, it seems that the formula of cell O1 is changed to formula result. Actually, it hides the formula with displaying the formula result in the Formula Bar. And the formula will display if the code is broken.
  • To post as a guest, your comment is unpublished.
    crystal · 3 years ago
    @ajay Dear ajay,
    For hidding formula of cells, please follow the below hyperlink to get the solution.
    https://www.extendoffice.com/documents/excel/1424-excel-hide-formulas.html
  • To post as a guest, your comment is unpublished.
    AJ · 3 years ago
    how could i lock a range of rows, say 4-46 and columns 8 and 10
  • To post as a guest, your comment is unpublished.
    ajay · 3 years ago
    I wanted to hide formula in Cell O1. Please inform formula for the same.