Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Lock cells after data entry ?

Hi, I have a shared excel file. I want to prevent users from deleting and
changing entries on it.
I only want them to be able to add new data to the empty cells.
I found a sample code to do that, but whenever I open the file ,
i need to re-run the macro to protect the cells that are not empty.

Sample Code :

Private Sub Worksheet_Change(ByVal Target As Range)

Dim cel As Range

If Not Intersect(Target, Me.[a1:ba2000]) Is Nothing Then
Application.EnableEvents = False
On Error Resume Next
Me.Unprotect "pw"
On Error GoTo 0
For Each cel In Intersect(Target, Me.[a1:ba2000]).Cells
cel.Locked = True
Next
ActiveSheet.EnableSelection = xlUnlockedCells
Me.Protect "pw"
Application.EnableEvents = True
End If

End Sub

Sub ini_lock()
Dim cel As Range

Application.EnableEvents = False
On Error Resume Next
ActiveSheet.Unprotect "pw"
On Error GoTo 0
Cells.Locked = False
For Each cel In Intersect([a1:ba2000], [a1:ba2000])
If cel.Value < "" Or cel.HasFormula = True Then cel.Locked =
True
Next
ActiveSheet.EnableSelection = xlUnlockedCells
ActiveSheet.Protect "pw"
Application.EnableEvents = True

End Sub

How can I make this macro automatically run , when I open the file?
I tried this code, but I still need to go to the macros menu and run macro
manually.

Private Sub Workbook_Open()
Run ini_lock
End Sub


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,071
Default Lock cells after data entry ?

Bugs
If all you want to do is prevent the user from altering the contents of
a cell that already has an entry, and allow an entry if the cell was blank,
I suggest the following macro and forget about locking/protecting cells and
sheets. HTH Otto
Private Sub Worksheet_Change(ByVal Target As Range)
Dim NewValue As Variant, OldValue As Variant
If Target.Count 1 Then Exit Sub
'Say you want to work with the range A1:F100
If Not Intersect(Target, Range("A1:F100")) Is Nothing Then
NewValue = Target.Value
Application.EnableEvents = False
Application.Undo
OldValue = Target.Value
If OldValue < "" Then
MsgBox "You cannot alter the contents of this cell.", 16,
"Invalid"
Target.Value = OldValue
Else
Target.Value = NewValue
End If
Application.EnableEvents = True
End If
End Sub

"Bugs" <celebimehmet«no wrote in message
...
Hi, I have a shared excel file. I want to prevent users from deleting and
changing entries on it.
I only want them to be able to add new data to the empty cells.
I found a sample code to do that, but whenever I open the file ,
i need to re-run the macro to protect the cells that are not empty.

Sample Code :

Private Sub Worksheet_Change(ByVal Target As Range)

Dim cel As Range

If Not Intersect(Target, Me.[a1:ba2000]) Is Nothing Then
Application.EnableEvents = False
On Error Resume Next
Me.Unprotect "pw"
On Error GoTo 0
For Each cel In Intersect(Target, Me.[a1:ba2000]).Cells
cel.Locked = True
Next
ActiveSheet.EnableSelection = xlUnlockedCells
Me.Protect "pw"
Application.EnableEvents = True
End If

End Sub

Sub ini_lock()
Dim cel As Range

Application.EnableEvents = False
On Error Resume Next
ActiveSheet.Unprotect "pw"
On Error GoTo 0
Cells.Locked = False
For Each cel In Intersect([a1:ba2000], [a1:ba2000])
If cel.Value < "" Or cel.HasFormula = True Then cel.Locked =
True
Next
ActiveSheet.EnableSelection = xlUnlockedCells
ActiveSheet.Protect "pw"
Application.EnableEvents = True

End Sub

How can I make this macro automatically run , when I open the file?
I tried this code, but I still need to go to the macros menu and run macro
manually.

Private Sub Workbook_Open()
Run ini_lock
End Sub



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Lock cells after data entry ?

Hi Mr. Otto, you're right.
I have an ftp site. I want to create an excel sheet on it for tracking file
uploads.
Users will open the file and then they will be add a record like this :
"i uploaded the xyx.txt file on 01.05.2009 - Bugs"

When they make this changes i want to protect old records..

I tried your macro but nothings happen :(
May be i making an error when i try to use it.
Could you please explain me how can i use it ?

I think now there was another big problem if i use vba to do this. If users
goes to vba editor menu, they can be found the protection password in the
vba code :)

Thanks for help & best regards..
Bugs


"Otto Moehrbach" wrote in message
...
Bugs
If all you want to do is prevent the user from altering the contents of
a cell that already has an entry, and allow an entry if the cell was
blank, I suggest the following macro and forget about locking/protecting
cells and sheets. HTH Otto
Private Sub Worksheet_Change(ByVal Target As Range)
Dim NewValue As Variant, OldValue As Variant
If Target.Count 1 Then Exit Sub
'Say you want to work with the range A1:F100
If Not Intersect(Target, Range("A1:F100")) Is Nothing Then
NewValue = Target.Value
Application.EnableEvents = False
Application.Undo
OldValue = Target.Value
If OldValue < "" Then
MsgBox "You cannot alter the contents of this cell.", 16,
"Invalid"
Target.Value = OldValue
Else
Target.Value = NewValue
End If
Application.EnableEvents = True
End If
End Sub

"Bugs" <celebimehmet«no wrote in message
...
Hi, I have a shared excel file. I want to prevent users from deleting and
changing entries on it.
I only want them to be able to add new data to the empty cells.
I found a sample code to do that, but whenever I open the file ,
i need to re-run the macro to protect the cells that are not empty.




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,071
Default Lock cells after data entry ?

Bugs
This macro is a sheet event macro and must be placed in the sheet module
of the pertinent sheet. As written, this macro will do what you want but
only if the changed cell is in the range A1:F100. I chose that range
arbitrarily. Change that in the macro as needed. To access that module,
right-click on the sheet tab and select View Code. Paste this macro in that
module. "X" out of the module to return to your sheet. There are no
passwords used in this macro. Be aware that the user must open the file
with macros enabled. HTH Otto
"Bugs" <celebimehmet«no wrote in message
...
Hi Mr. Otto, you're right.
I have an ftp site. I want to create an excel sheet on it for tracking
file uploads.
Users will open the file and then they will be add a record like this :
"i uploaded the xyx.txt file on 01.05.2009 - Bugs"

When they make this changes i want to protect old records..

I tried your macro but nothings happen :(
May be i making an error when i try to use it.
Could you please explain me how can i use it ?

I think now there was another big problem if i use vba to do this. If
users goes to vba editor menu, they can be found the protection password
in the vba code :)

Thanks for help & best regards..
Bugs


"Otto Moehrbach" wrote in message
...
Bugs
If all you want to do is prevent the user from altering the contents
of a cell that already has an entry, and allow an entry if the cell was
blank, I suggest the following macro and forget about locking/protecting
cells and sheets. HTH Otto
Private Sub Worksheet_Change(ByVal Target As Range)
Dim NewValue As Variant, OldValue As Variant
If Target.Count 1 Then Exit Sub
'Say you want to work with the range A1:F100
If Not Intersect(Target, Range("A1:F100")) Is Nothing Then
NewValue = Target.Value
Application.EnableEvents = False
Application.Undo
OldValue = Target.Value
If OldValue < "" Then
MsgBox "You cannot alter the contents of this cell.", 16,
"Invalid"
Target.Value = OldValue
Else
Target.Value = NewValue
End If
Application.EnableEvents = True
End If
End Sub

"Bugs" <celebimehmet«no wrote in message
...
Hi, I have a shared excel file. I want to prevent users from deleting
and changing entries on it.
I only want them to be able to add new data to the empty cells.
I found a sample code to do that, but whenever I open the file ,
i need to re-run the macro to protect the cells that are not empty.






  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Lock cells after data entry ?


Otto,

Your code indeed works for a cell has focus however, the cell contents
can be overwritten when I select a range which includes this cell, clear
the contents then insert new data.


--
greman
------------------------------------------------------------------------
greman's Profile: http://www.thecodecage.com/forumz/member.php?userid=498
View this thread: http://www.thecodecage.com/forumz/sh...ad.php?t=95482

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I lock a cell after data entry so entry can not be changed Verlinde Excel Discussion (Misc queries) 3 April 22nd 10 07:59 PM
Lock format but allow data entry Iriemon Excel Discussion (Misc queries) 0 April 7th 10 07:30 PM
lock cells after entry maximillan via OfficeKB.com Excel Programming 1 April 29th 07 04:34 AM
Lock Row after Data Entry boatnisfun Excel Discussion (Misc queries) 1 March 10th 07 08:03 AM
Lock Cell After Data Entry Ripper Excel Discussion (Misc queries) 1 February 28th 06 08:17 PM


All times are GMT +1. The time now is 09:44 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"