Hello I used this code last week and it worked perfectly but now is not working. Can anyone see what I am doing wrong ?
Private Sub Worksheet_Change(ByVal Target As Range)
Dim oldVal As String, newVal As String
On Error GoTo exitHandler
'---Test to see if code should continue.
' Test statements could be combined, and are
' shown separately to simplify modification
If Target.Count > 1 Or Target.Text = "" Then Exit Sub
If Target.SpecialCells(xlCellTypeSameValidation) _
Is Nothing Then Exit Sub
'---modify to any valid range reference on activesheet
If Intersect(Target, Range("K:O")) _
Is Nothing Then Exit Sub
Application.EnableEvents = False
newVal = Target.Text
Application.Undo
oldVal = Target.Text
Target.Value = newVal
If oldVal = "" Then GoTo exitHandler
If oldVal = newVal Then
Target.Value = ""
ElseIf InStr(1, oldVal, newVal) > 0 Then
If Right(oldVal, Len(newVal)) = newVal Then
Target.Value = Left(oldVal, Len(oldVal) - Len(newVal) - 1)
Else
Target.Value = Replace(oldVal, newVal & Chr(10), "")
End If
Else
Target.Value = oldVal & Chr(10) & newVal
End If
exitHandler:
Application.EnableEvents = True
End Sub
</pre>
Private Sub Worksheet_Change(ByVal Target As Range)
Dim oldVal As String, newVal As String
On Error GoTo exitHandler
'---Test to see if code should continue.
' Test statements could be combined, and are
' shown separately to simplify modification
If Target.Count > 1 Or Target.Text = "" Then Exit Sub
If Target.SpecialCells(xlCellTypeSameValidation) _
Is Nothing Then Exit Sub
'---modify to any valid range reference on activesheet
If Intersect(Target, Range("K:O")) _
Is Nothing Then Exit Sub
Application.EnableEvents = False
newVal = Target.Text
Application.Undo
oldVal = Target.Text
Target.Value = newVal
If oldVal = "" Then GoTo exitHandler
If oldVal = newVal Then
Target.Value = ""
ElseIf InStr(1, oldVal, newVal) > 0 Then
If Right(oldVal, Len(newVal)) = newVal Then
Target.Value = Left(oldVal, Len(oldVal) - Len(newVal) - 1)
Else
Target.Value = Replace(oldVal, newVal & Chr(10), "")
End If
Else
Target.Value = oldVal & Chr(10) & newVal
End If
exitHandler:
Application.EnableEvents = True
End Sub
</pre>