Solved

lock specific cells once cell is no longer blank with VBA

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

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 31

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 17

Expert Comment

by:Roy_Cox
ID: 41778341
Pleased to help
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now