Solved

lock specific cells once cell is no longer blank with VBA

Posted on 2016-08-30
4
34 Views
Last Modified: 2016-08-31
Can an expert provide me with the code I need to be able to lock specific cells once they go from blank to non blank.

i.e. D4 - F4, D7-F7, D12 - F12

Many thanks
0
Comment
Question by:Jagwarman
  • 2
4 Comments
 
LVL 18

Accepted Solution

by:
Roy_Cox earned 500 total points
ID: 41777511
Try this code.

Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Const PW As String = "Secret"
    If Not Intersect(Target, Range("D4:F4, D7:F7, D12:F12")) Is Nothing Then
        ActiveSheet.Unprotect PW
        If Not IsEmpty(Target) Then Target.Locked = True
        ActiveSheet.Protect PW
    End If
End Sub

Open in new window


Before using it you must first unlock all the specified cells.

This is triggered by a worksheet event and should be in the worksheet module, see this for instructions of where to place the code if you are not sure
AutoLock.xlsm
0
 
LVL 33

Expert Comment

by:Rob Henson
ID: 41777713
FYI- Running any VBA clears the Undo history.

Therefore, once the cells are locked, the only way to undo would be to reverse the effect manually or by using another VBA routine to unlock the ranges or chosen cells.
1
 

Author Closing Comment

by:Jagwarman
ID: 41778338
Thanks Roy works perfect for me
0
 
LVL 18

Expert Comment

by:Roy_Cox
ID: 41778341
Pleased to help
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question