Solved

lock specific cells once cell is no longer blank with VBA

Posted on 2016-08-30
4
33 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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 …
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

840 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