Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 287
Default Allowing entry only once

I have the following code which prevents the user to edit the cell A1 more
than once

Private Sub Worksheet_Change(ByVal Target As Range)
Dim Original As Variant
Dim NewVal As Variant
If Target.Count 1 Then Exit Sub
If Target.Address = "$A$1" Then
NewVal = Target.Value
Application.EnableEvents = False
Application.Undo
Original = Target.Value
If Original = "" Then
Target = NewVal
Else
MsgBox "No change to this cell is allowed"
End If
Application.EnableEvents = True
End If
End Sub

How do I edit this code to make it work for more than A1 i.e. A1:A100
Thanks
Adam
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 23
Default Allowing entry only once

Have a look at Intersect in help. You could test if the Target address
intersects with your desired range, then do your tests for a first time edit.

"Adam" wrote:

I have the following code which prevents the user to edit the cell A1 more
than once

Private Sub Worksheet_Change(ByVal Target As Range)
Dim Original As Variant
Dim NewVal As Variant
If Target.Count 1 Then Exit Sub
If Target.Address = "$A$1" Then
NewVal = Target.Value
Application.EnableEvents = False
Application.Undo
Original = Target.Value
If Original = "" Then
Target = NewVal
Else
MsgBox "No change to this cell is allowed"
End If
Application.EnableEvents = True
End If
End Sub

How do I edit this code to make it work for more than A1 i.e. A1:A100
Thanks
Adam

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,758
Default Allowing entry only once

Have you thought of protecting the worksheet (unlock the cells the users can
change).

Then you could have your code check the ranges that can be changed only once.
If a change is made, you unprotect the worksheet, lock that cell and reprotect
the worksheet. And that locked cell can't be changed.

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim myChangeOnceCells As Range
Dim myCell As Range
Dim pwd As String

pwd = "hi"

Set myChangeOnceCells = Me.Range("A1,A3,A6,D7,C3,B4,B6")

If Intersect(Target.Cells, myChangeOnceCells) Is Nothing Then Exit Sub

Me.Unprotect Password:=pwd
For Each myCell In Intersect(Target.Cells, myChangeOnceCells).Cells
myCell.Locked = True
Next myCell
Me.Protect Password:=pwd

End Sub

Another option may be to use a hidden sheet. With each initial change, write
that value to the hidden sheet (same address). Then always compare before
accepting/rejecting the proposed changes.

Adam wrote:

I have the following code which prevents the user to edit the cell A1 more
than once

Private Sub Worksheet_Change(ByVal Target As Range)
Dim Original As Variant
Dim NewVal As Variant
If Target.Count 1 Then Exit Sub
If Target.Address = "$A$1" Then
NewVal = Target.Value
Application.EnableEvents = False
Application.Undo
Original = Target.Value
If Original = "" Then
Target = NewVal
Else
MsgBox "No change to this cell is allowed"
End If
Application.EnableEvents = True
End If
End Sub

How do I edit this code to make it work for more than A1 i.e. A1:A100
Thanks
Adam


--

Dave Peterson
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
Locking from data entry but allowing oulining to function? saharafrog Excel Discussion (Misc queries) 1 March 26th 10 08:29 PM
Allowing data entry in protected sheets David Excel Discussion (Misc queries) 1 November 7th 06 09:18 PM
exel 2003 suddenly stopped allowing data entry Michael Excel Discussion (Misc queries) 0 October 25th 06 02:42 AM
Allowing Data Entry ONLY??? mthespike Excel Worksheet Functions 1 June 29th 05 08:11 PM
Allowing only one entry in a range McKenna New Users to Excel 7 March 11th 05 04:56 PM


All times are GMT +1. The time now is 08:24 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"