Can Worksheet Code Fire Before Data Validation?

6

6afraidbecause789

Why does data validation conflict with or not allow this worksheet
code to run? The first part of this thread was titled "Speed-Up Data
Entry by having Excel Complete Strings Options" and was answered by
Dave and Paul (their code below). However, their code only works in
cells that do not have the validation.

I'm validating cells that must contain any combination of 3 digits (a
1, 2, 3, or 4) or a 0. If anything but a 0, 111, 222, 333, 444, 413,
420, etc. is entered in a cell, a validation msg appears. For
instance, if I type a 4 and press enter, the data validation fires
before the code.

Is there a way to do this validation with VB instead of using Excel's
menu? The code that Dave and Paul provided to help users quickly auto
enter the 3-digit combinations were:

'From Dave
Option Explicit
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
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

'From Paul
'Private Sub Worksheet_Change(ByVal Target As Range)
'If Target Like "#" And Len(Target) = 1 Then
'Target = 1 * (Target & Target & Target)
'End If
'End Sub

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top