Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in
x
or
x
x
Register
x

or

How to lock specified cells without protecting the whole worksheet in Excel?

Normally, you need to protect the whole worksheet for locking cells from editing. Is there any method to lock cells without protecting the whole worksheet? This article recommend a VBA method for you.

Lock specified cells without protecting the whole worksheet with VBA

Office Tab Enable Tabbed Editing and Browsing in Office, and Make Your Work Much Easier...
Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%
  • Reuse Anything: Add the most used or complex formulas, charts and anything else to your favorites, and quickly reuse them in the future.
  • More than 20 text features: Extract Number from Text String; Extract or Remove Part of Texts; Convert Numbers and Currencies to English Words.
  • Merge Tools: Multiple Workbooks and Sheets into One; Merge Multiple Cells/Rows/Columns Without Losing Data; Merge Duplicate Rows and Sum.
  • Split Tools: Split Data into Multiple Sheets Based on Value; One Workbook to Multiple Excel, PDF or CSV Files; One Column to Multiple Columns.
  • Paste Skipping Hidden/Filtered Rows; Count And Sum by Background Color; Send Personalized Emails to Multiple Recipients in Bulk.
  • Super Filter: Create advanced filter schemes and apply to any sheets; Sort by week, day, frequency and more; Filter by bold, formulas, comment...
  • More than 300 powerful features; Works with Office 2007-2019 and 365; Supports all languages; Easy deploying in your enterprise or organization.

arrow blue right bubbleLock specified cells without protecting the whole worksheet with VBA


Supposing you need to lock cell A3 and A5 in current worksheet, the following VBA code will help you achieve it without protecting the whole worksheet.

1. Right click the sheet tab, and select View Code from the right-clicking menu.

2. Then copy and paste the below VBA code into the Code window. See screenshot:

VBA code: Lock specified cells without protecting the whole worksheet

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

Note: In the code, Column 1, Row = 3 and Row = 5 indicate cell A3 and A5 in current worksheet will be locked after running the code. You can change them as you need.

3. Press the Alt + Q keys simultaneously to close the Microsoft Visual Basic for Applications window.

Now cell A3 and A5 are locked in current worksheet. If you try to select cell A3 or A5 in current worksheet, the cursor will be moved to the right adjacent cell automatically.


arrow blue right bubbleRelated articles:


Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%

  • Reuse: Quickly insert complex formulas, charts and anything that you have used before; Encrypt Cells with password; Create Mailing List and send emails...
  • Super Formula Bar (easily edit multiple lines of text and formula); Reading Layout (easily read and edit large numbers of cells); Paste to Filtered Range...
  • Merge Cells/Rows/Columns without losing Data; Split Cells Content; Combine Duplicate Rows/Columns... Prevent Duplicate Cells; Compare Ranges...
  • Select Duplicate or Unique Rows; Select Blank Rows (all cells are empty); Super Find and Fuzzy Find in Many Workbooks; Random Select...
  • Exact Copy Multiple Cells without changing formula reference; Auto Create References to Multiple Sheets; Insert Bullets, Check Boxes and more...
  • Extract Text, Add Text, Remove by Position, Remove Space; Create and Print Paging Subtotals; Convert Between Cells Content and Comments...
  • Super Filter (save and apply filter schemes to other sheets); Advanced Sort by month/week/day, frequency and more; Special Filter by bold, italic...
  • Combine Workbooks and WorkSheets; Merge Tables based on key columns; Split Data into Multiple Sheets; Batch Convert xls, xlsx and PDF...
  • More than 300 powerful features. Supports Office/Excel 2007-2019 and 365. Supports all languages. Easy deploying in your enterprise or organization. Full features 30-day free trial.
kte tab 201905

Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

  • Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
officetab bottom
Say something here...
symbols left.
You are guest ( Sign Up? )
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.
    Kasey · 5 months 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.
    Charlie · 10 months 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.
      crystal · 9 months ago
      Hi Charlie,
      Sorry can't help you with that yet. Thank you for your comment.
  • To post as a guest, your comment is unpublished.
    Espen · 10 months 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 · 1 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.
    Erin · 1 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 · 1 years ago
      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.
    Carlos · 1 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 · 1 years ago
      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.
    Angel Pablo Izquierdo Molino · 2 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 · 2 years ago
      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.
    AJ · 2 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.
      crystal · 2 years ago
      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.
    ajay · 2 years ago
    I wanted to hide formula in Cell O1. Please inform formula for the same.
    • To post as a guest, your comment is unpublished.
      crystal · 2 years ago
      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 · 2 years ago
      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