If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Excel > lock and unlock cell

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-13-04, 08:26
Alexxx12 Alexxx12 is offline
Registered User
 
Join Date: Sep 2002
Location: NJ
Posts: 139
lock and unlock cell

Hi,

excel 2k
windows2k

I have an excel spreadsheet in which the user will click a commandbutton_click event to enter data either being copied from already existing data from another cell or entered manually. The cell, or range of cells, say: range("A1:C10"), are unlocked after the spreadsheet has been locked for all other cells. I want the cell to be locked right after the data is entered so that the user can not alter the data. Or is there a way to have the entire sheet locked and then when the user click the command button, the cell is unlocked, the data entered and then when the user presses enter, the cell is locked?

Private Sub CommandButton1_Click()
Dim i As Integer
For i = 1 To 10
Range("A" & i).Select
Range("A" & i).Locked = False
Range("A" & i).Value = Range("H" & i).Value
Range("A" & i).Select
Selection.Locked = True

Range("c" & i).Select
Selection.Locked = False
Range("c" & i).Value = Date
Range("c" & i).Select
Selection.Locked = True
'Range("c" & i).Locked = True
Next i
End Sub


This does not work. Any help is greatly appreciated.
Reply With Quote
  #2 (permalink)  
Old 08-13-04, 09:18
DavidCoutts DavidCoutts is offline
Registered User
 
Join Date: Jan 2004
Location: Aberdeen, Scotland
Posts: 1,067
what i think you need to look at is the Worksheet_Change Event
something like
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    ActiveSheet.Unprotect
    If Not Target.Locked Then
        If Not Target.Formula = "" Then
            Target.Locked = True
            ActiveSheet.Protect
        End If
    End If
End Sub
HTH
Dave
Reply With Quote
  #3 (permalink)  
Old 08-13-04, 09:43
shades shades is offline
Registered User
 
Join Date: Oct 2003
Posts: 1,091
Leave the cell Locked at all times. The key is to unProtect the beginning of that section of the macro, then Protect at the end. This way you don't need to have a separate Private sub.
__________________
old, slow, and confused
but at least I'm inconsistent!

Rich
(retired Excel 2003 user, 3/28/2008)

How to ask a question on forums
Reply With Quote
  #4 (permalink)  
Old 08-18-04, 16:49
Alexxx12 Alexxx12 is offline
Registered User
 
Join Date: Sep 2002
Location: NJ
Posts: 139
lock cell in Excel

Hi,

neither option really works. Here is the code I want to execute:

I want the sheet to be locked, but when clicking the button, then i want to be able to ulock that cell, apply the data and then lock the cell again so that it can not be touched.

Private Sub CommandButton1_Click()
Dim i As Integer
For i = 1 To 10
Range("a" & i).Value = "This is a test " & i
Next i
End Sub
Reply With Quote
  #5 (permalink)  
Old 08-18-04, 17:04
shades shades is offline
Registered User
 
Join Date: Oct 2003
Posts: 1,091
You don't need this in the Private code.

Rather, put this in the general Module:
Code:
Sub CommandButton1_Click()
    Dim i As Integer
    ActiveSheet.Unprotect
    For i = 1 To 10
        Range("a" & i).Value = "This is a test " & i
    Next i
    ActiveSheet.Protect

End Sub
Then assign this macro to the commandbutton. If you want to have it password protected, then add appropriate items.

I tried this and it works fine.
__________________
old, slow, and confused
but at least I'm inconsistent!

Rich
(retired Excel 2003 user, 3/28/2008)

How to ask a question on forums
Reply With Quote
  #6 (permalink)  
Old 08-20-04, 16:24
Alexxx12 Alexxx12 is offline
Registered User
 
Join Date: Sep 2002
Location: NJ
Posts: 139
lock cell

HI,

That works well if the whole sheet is to be protected. but my problem is that the user should be able to modify some cell and not others.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On