Hi
I apologise if this has already been solved somewhere on this forum, however I am unable to find it.
I have two workbooks in which I am trying to use the same code. In one it is working but in the other it is not. I am using Excel 2007.
Here is the code:
Option Explicit
' Developed by Contextures Inc.
' www.contextures.com
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
Dim lUsed As Long
Dim lOld As Long
If Target.Count > 1 Then GoTo exitHandler
On Error Resume Next
Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)
On Error GoTo exitHandler
If rngDV Is Nothing Then GoTo exitHandler
If Intersect(Target, rngDV) Is Nothing Then
'do nothing
Else
Application.EnableEvents = False
newVal = Target.Value
Application.Undo
oldVal = Target.Value
Target.Value = newVal
If Target.Column = 3 Then
If oldVal = "" Then
'do nothing
Else
If newVal = "" Then
'do nothing
Else
lOld = Len(oldVal)
If Left(newVal, lOld) = oldVal Then
Target.Value = newVal
Else
lUsed = InStr(1, oldVal, newVal)
If lUsed > 0 Then
Target.Value = oldVal
Else
Target.Value = oldVal _
& ", " & newVal
End If
End If
End If
End If
End If
End If
exitHandler:
Application.EnableEvents = True
End Sub
Thanks
I apologise if this has already been solved somewhere on this forum, however I am unable to find it.
I have two workbooks in which I am trying to use the same code. In one it is working but in the other it is not. I am using Excel 2007.
Here is the code:
Option Explicit
' Developed by Contextures Inc.
' www.contextures.com
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
Dim lUsed As Long
Dim lOld As Long
If Target.Count > 1 Then GoTo exitHandler
On Error Resume Next
Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)
On Error GoTo exitHandler
If rngDV Is Nothing Then GoTo exitHandler
If Intersect(Target, rngDV) Is Nothing Then
'do nothing
Else
Application.EnableEvents = False
newVal = Target.Value
Application.Undo
oldVal = Target.Value
Target.Value = newVal
If Target.Column = 3 Then
If oldVal = "" Then
'do nothing
Else
If newVal = "" Then
'do nothing
Else
lOld = Len(oldVal)
If Left(newVal, lOld) = oldVal Then
Target.Value = newVal
Else
lUsed = InStr(1, oldVal, newVal)
If lUsed > 0 Then
Target.Value = oldVal
Else
Target.Value = oldVal _
& ", " & newVal
End If
End If
End If
End If
End If
End If
exitHandler:
Application.EnableEvents = True
End Sub
Thanks