Share via

Is it possible to modify VBA coding for Multiple selection drop down lists to achieve the following.

Anonymous
2024-02-21T14:09:29+00:00

I found some VBA code to create multiple selection drop down list in excel cell. The existing coding prevents a selection from being added twice. Works great but not quite what I need. I am using the cell to allocate engineers to a project. Unfortunately, this cell needs constantly updating depending on who is available. For example if Joe Bloggs is sick he will need replacing by another engineer. As it stands, the only way to do this is to delete the contents of the cell and start again. What I really need is if a name is clicked in the drop down list a second time, it is removed from the list. I'm a beginner in VBA so any help greatly appreciated.

This is the existing code:

"Private Sub Worksheet_Change(ByVal Target As Range)
Dim Oldvalue As String
Dim Newvalue As String
Application.EnableEvents = True
On Error GoTo Exitsub
If Not Intersect(Target, Range("J:K")) Is Nothing Then
If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
GoTo Exitsub
Else: If Target.Value = "" Then GoTo Exitsub Else
Application.EnableEvents = False
Newvalue = Target.Value
Application.Undo
Oldvalue = Target.Value
If Oldvalue = "" Then
Target.Value = Newvalue
Else
If InStr(1, Oldvalue, Newvalue) = 0 Then
Target.Value = Oldvalue & vbNewLine & Newvalue
Else:
Target.Value = Oldvalue
End If
End If
End If
End If
Application.EnableEvents = True
Exitsub:
Application.EnableEvents = True
End Sub"

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

3 answers

Sort by: Most helpful
  1. HansV 462.6K Reputation points
    2024-02-21T15:07:49+00:00

    Like this:

    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim OldValue As String
        Dim NewValue As String
        If Target.CountLarge > 1 Then Exit Sub
        If Intersect(Target, Range("J:K")) Is Nothing Then Exit Sub
        If Target.Value = "" Then Exit Sub
        Application.EnableEvents = False
        On Error GoTo ExitSub
        NewValue = Target.Value
        Application.Undo
        OldValue = Target.Value
        If OldValue = "" Then
            Target.Value = NewValue
        ElseIf InStr(1, vbLf & OldValue & vbLf, vbLf & NewValue & vbLf) = 0 Then
            Target.Value = OldValue & vbLf & NewValue
        Else
            NewValue = Replace(vbLf & OldValue & vbLf, vbLf & NewValue & vbLf, vbLf)
            If Left(NewValue, 1) = vbLf Then
                NewValue = Mid(NewValue, 2)
            End If
            If Right(NewValue, 1) = vbLf Then
                NewValue = Left(NewValue, Len(NewValue) - 1)
            End If
            Target.Value = NewValue
        End If
    ExitSub:
        Application.EnableEvents = True
    End Sub
    

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2024-02-22T09:03:45+00:00

    Perfect! Just what I was looking for. Thank you so much.

    Just need to digest the code though to understand exactly what is happening in each process.

    Best Regards

    Was this answer helpful?

    0 comments No comments
  3. Andreas Killer 144.1K Reputation points Volunteer Moderator
    2024-02-21T15:04:24+00:00

    Was this answer helpful?

    0 comments No comments