Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function IsIn(x, arr) As Boolean
- Dim found As Boolean
- found = False
- For Each elem In arr
- If elem = x Then
- found = True
- Exit For
- End If
- Next
- IsIn = found
- End Function
- Private Sub Worksheet_Change(ByVal Target As Range)
- ' Developed by Contextures Inc.
- ' www.contextures.com
- ' Edited and extended by Sebastian Hegardt
- ' --- Declare Variables ---
- Dim rngDV As Range
- Dim oldVal As String
- Dim newVal As String
- Dim column As Integer
- Static columns As Variant
- ' --- Declare constants ---
- 'Separator string to use. Use vbNewLine for separation by new line
- Const separator As String = ", "
- 'Array of columns to apply this script to
- columns = Array(3, 5)
- 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 'Save current new value
- Application.Undo
- oldVal = Target.Value 'Undo and save old value
- Target.Value = newVal
- If IsIn(Target.column, columns) Then
- If oldVal = "" Then
- 'do nothing
- ElseIf newVal = "" Then 'Nothing was selected
- 'do nothing
- Else
- Target.Value = oldVal & separator & newVal
- End If
- End If
- End If
- exitHandler:
- Application.EnableEvents = True
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment