Solved

lock specific cells once cell is no longer blank with VBA

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

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

772 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