Advertisement
Guest User

List

a guest
Jun 25th, 2018
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ASP 1.15 KB | None | 0 0
  1. Private Sub Worksheet_Change(ByVal Target As Range)
  2.  
  3. Dim oldValue As String
  4. ' The previous value.
  5. Dim newValue As String
  6. ' The new value.
  7. Dim updateValue As String
  8. ' The potentially new value, subject to tests.
  9.  
  10. Application.EnableEvents = True
  11. On Error GoTo Exitsub
  12.        
  13. If Target.Column = 4 Then
  14.     If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
  15.         GoTo Exitsub
  16.     Else: If Target.Value = "" Then GoTo Exitsub Else
  17.         Application.EnableEvents = False
  18.         updateValue = Target.Value
  19.         Application.Undo
  20.         oldValue = Target.Value
  21.             If oldValue = "" Then
  22.                 updateValue = newValue
  23.                 Target.Value = newValue
  24.             Else
  25.                 If InStr(1, oldValue, newValue, vbTextCompare) Then
  26.                     Target.Value = Trim(Replace(oldValue, newValue, ""))
  27.                     newValue = updateValue
  28.                     Target.Value = newValue
  29.             Else:
  30.                 Target.Value = oldValue & ", " & newValue
  31.             End If
  32.         End If
  33.     End If
  34. End If
  35.  
  36. Application.EnableEvents = True
  37. Exitsub:
  38. Application.EnableEvents = True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement