Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Private Sub Worksheet_Change(ByVal Target As Range)
- Dim oldValue As String
- ' The previous value.
- Dim newValue As String
- ' The new value.
- Dim updateValue As String
- ' The potentially new value, subject to tests.
- Application.EnableEvents = True
- On Error GoTo Exitsub
- If Target.Column = 4 Then
- If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
- GoTo Exitsub
- Else: If Target.Value = "" Then GoTo Exitsub Else
- Application.EnableEvents = False
- updateValue = Target.Value
- Application.Undo
- oldValue = Target.Value
- If oldValue = "" Then
- updateValue = newValue
- Target.Value = newValue
- Else
- If InStr(1, oldValue, newValue, vbTextCompare) Then
- Target.Value = Trim(Replace(oldValue, newValue, ""))
- newValue = updateValue
- Target.Value = newValue
- Else:
- Target.Value = oldValue & ", " & newValue
- End If
- End If
- End If
- End If
- Application.EnableEvents = True
- Exitsub:
- Application.EnableEvents = True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement