Solved

lock specific cells once cell is no longer blank with VBA

Posted on 2016-08-30
4
36 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 20

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 20

Expert Comment

by:Roy_Cox
ID: 41778341
Pleased to help
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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 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.

707 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