Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Excel:  Protect Cell After Data Entry

Posted on 2014-08-12
5
Medium Priority
?
3,038 Views
Last Modified: 2016-09-27
I have a spreadsheet with columns B and L.  For any entry in columns B or L, I would like the cell to be protected as soon as input complete.

So, if a value is entered into B2, it should be protected.  But when the user moves to B3, that cell should be still open for input while B2 is locked.

Can someone help?
0
Comment
Question by:mrherndon
5 Comments
 
LVL 18

Expert Comment

by:krishnakrkc
ID: 40255936
In the sheet module

Option Explicit

Dim blnUnlockedAllCells As Boolean
Private Sub Worksheet_Change(ByVal Target As Range)
    
    Const RangeToLock As String = "B:B,L:L" '<<  adjust to suit
    
    If Target.Cells.Count > 1 Then Exit Sub
    
    If Not blnUnlockedAllCells Then
        Me.Cells.Locked = False
        On Error Resume Next
        Me.Range(CStr(RangeToLock)).SpecialCells(2).Locked = True
        On Error GoTo 0
        blnUnlockedAllCells = True
        Me.Protect Password:="pwd", userinterfaceonly:=True
    End If
    
    If Not Application.Intersect(Target, Me.Range(CStr(RangeToLock))) Is Nothing Then
        If Len(Target) Then Target.Locked = True
    End If
    
End Sub

Open in new window


Kris
0
 
LVL 34

Expert Comment

by:Rob Henson
ID: 40256011
Using Worksheet Change Event is good in some cases but the downside is that you lose your Undo History.

So in this case, what happens if the users realises that they have entered the wrong value in B2? The Change Event locks it and they can't change it. They can't just do an Undo either because a macro has run.

Thanks
Rob H
0
 

Author Comment

by:mrherndon
ID: 40256784
Thanks very much.  I see where the sheet module example given could work well for me.

Question:  how best to combine two different subroutines on the same sheet module?  I have another in place now, which I use to control point of focus.  How best to combine the below with the proposed solution?

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.Count = 1 Then
        Application.EnableEvents = False
        If Target.Column = 1 Then
            Target.Offset(0, 1).Value = VBA.Now
            Target.Offset(0, 10).Select
        ElseIf Target.Column = 11 Then
            Target.Offset(1, -10).Select
        End If
        Application.EnableEvents = True
    End If
End Sub

Open in new window

0
 
LVL 18

Accepted Solution

by:
krishnakrkc earned 2000 total points
ID: 40257375
Option Explicit
Dim blnUnlockedAllCells As Boolean
Private Sub Worksheet_Change(ByVal Target As Range)
    
    Const RangeToLock As String = "B:B,L:L" '<<  adjust to suit
    
    If Target.Cells.Count > 1 Then Exit Sub
    
    Application.EnableEvents = False
    If Target.Column = 1 Then
        Target.Offset(0, 1).Value = VBA.Now
        Target.Offset(0, 1).Locked = True
        Target.Offset(0, 10).Select
    ElseIf Target.Column = 11 Then
        Target.Offset(1, -10).Select
    ElseIf Not Application.Intersect(Target, Me.Range(CStr(RangeToLock))) Is Nothing Then
        If Len(Target) Then Target.Locked = True
    End If
    Application.EnableEvents = True
    
    If Not blnUnlockedAllCells Then
        Me.Cells.Locked = False
        On Error Resume Next
        Me.Range(CStr(RangeToLock)).SpecialCells(2).Locked = True
        On Error GoTo 0
        blnUnlockedAllCells = True
        Me.Protect Password:="pwd", userinterfaceonly:=True
    End If
    
End Sub

Open in new window


Kris
0
 

Expert Comment

by:Pravallika p
ID: 41819198
Hello,

I have spreadsheet with range of A3:AN219, i would like the cell protected as soon as input complete.
I tried above solution but i did not get any solution for my problem.

Please help me on this
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

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

This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
No other job is as rewarding and demanding as building an iPhone app is. It is not really in the hands of the developer for the success of an iPhone app. Many factors operate jointly for every iOS application's success in the market.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
Starting up a Project

580 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