View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Rick Rothstein Rick Rothstein is offline
external usenet poster
 
Posts: 5,934
Default Validation of text cells through Code

You are not taking any action against a bad entry, so it remains. Consider
something like this...

Private Sub Worksheet_Change(ByVal Target As Range)
Dim myCell As Range
Dim myIntersect As Range
Set myIntersect = Intersect(Target, Me.Range("au13:ez100"))
If myIntersect Is Nothing Then Exit Sub
On Error Resume Next 'just fly by errors
Application.EnableEvents = False
For Each myCell In myIntersect.Cells
myCell = Left(myCell, 3)
If myCell Like "[0-4][0-4][0-4]" And Not myCell Like "*0*0*" Then
myCell.Value = String(3, CStr(myCell.Value))
Else
'
' I'm not sure what action you want to take when an invalid
' entry is made, but whatever you want to do, it should be
' done here. My example suggestion is to delete the entry.
'
myCell.Value = ""
End If
Next myCell
Application.EnableEvents = True
On Error GoTo 0
End Sub

--
Rick (MVP - Excel)


wrote in message
...
The cells are still accepting 3-digit combinations with numbers
greater than 4 (like 555, 426, 777). What code could block the entry
of numbers greater than 4?


Private Sub Worksheet_Change(ByVal Target As Range)

Dim myRngToInspect As Range
Dim myIntersect As Range
Dim myCell As Range

Set myRngToInspect = Me.Range("au13:ez100")

Set myIntersect = Intersect(Target, myRngToInspect)

If myIntersect Is Nothing Then
Exit Sub
End If

On Error Resume Next 'just fly by errors
Application.EnableEvents = False
For Each myCell In myIntersect.Cells

myCell = Left(myCell, 3)
If myCell Like "[0-4][0-4][0-4]" And Not myCell Like "*0*0*" Then
' That is a valid number
Else
' That number is not valid
End If

If myCell.Value Like "#" Then
myCell.Value = String(3, CStr(myCell.Value))
End If
Next myCell
Application.EnableEvents = True
On Error GoTo 0

End Sub

======================

Thanks