Deselect from Multiselect Dropdown (with vbNewLine)

GHVDS

New Member
Joined
Nov 1, 2022
Messages
4
Office Version
  1. 2010
Platform
  1. Windows
Hi all,

I am trying to understand VBA as I am working on my document. I am getting stuck at the following issue:

For two cells (D43 and D56)I want to be able to deselect values from a multi-select dropdown which presents each value below the other. My current code is as following (without deselect):

VBA 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("D56", "D43")) 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

How do I introduce the deselect function in here? Your help is much appreciated!
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Try this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim OldValue As String
    Dim NewValue As String
    Dim p1 As Long, p2 As Long

    If Intersect(Target, Range("D56", "D43")) Is Nothing Then Exit Sub
    If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then Exit Sub
    If Target.Value = "" Then Exit Sub
    
    Application.EnableEvents = False
    
    NewValue = Target.Value
    Application.Undo
    OldValue = Target.Value
    
    If OldValue = "" Then
        Target.Value = NewValue
    Else
        p1 = InStr(vbNewLine & OldValue & vbNewLine, vbNewLine & NewValue & vbNewLine)
        If p1 = 0 Then
            'Append new value
            Target.Value = OldValue & vbNewLine & NewValue
        Else
            'Remove already selected value
            p2 = p1 + Len(vbNewLine & NewValue & vbNewLine)
            If p1 = 1 Then
                'Remove from start
                OldValue = Mid(OldValue, p2 - 2)
            ElseIf p2 = Len(vbNewLine & OldValue & vbNewLine) + 1 Then
                'Remove from end
                OldValue = Left(OldValue, p1 - 3)
            ElseIf p1 > 1 Then
                'Remove from middle
                OldValue = Left(OldValue, p1 - 1) & Mid(OldValue, p2 - 2)
            End If
            Target.Value = OldValue
        End If
    End If
    
    Application.EnableEvents = True

End Sub
Notice that both InStr arguments are surrounded with vbNewLine characters to ensure that the correct value is found when deselecting a value. The p1 and p2 arguments in the subsequent Mid and Left functions account for the extra vbNewLines. Without this, if the dropdown list contained Bobby and Bob in that order and you select Bob to deselect it then Bobby would be removed instead.
 
Upvote 0
Solution
Try this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim OldValue As String
    Dim NewValue As String
    Dim p1 As Long, p2 As Long

    If Intersect(Target, Range("D56", "D43")) Is Nothing Then Exit Sub
    If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then Exit Sub
    If Target.Value = "" Then Exit Sub
   
    Application.EnableEvents = False
   
    NewValue = Target.Value
    Application.Undo
    OldValue = Target.Value
   
    If OldValue = "" Then
        Target.Value = NewValue
    Else
        p1 = InStr(vbNewLine & OldValue & vbNewLine, vbNewLine & NewValue & vbNewLine)
        If p1 = 0 Then
            'Append new value
            Target.Value = OldValue & vbNewLine & NewValue
        Else
            'Remove already selected value
            p2 = p1 + Len(vbNewLine & NewValue & vbNewLine)
            If p1 = 1 Then
                'Remove from start
                OldValue = Mid(OldValue, p2 - 2)
            ElseIf p2 = Len(vbNewLine & OldValue & vbNewLine) + 1 Then
                'Remove from end
                OldValue = Left(OldValue, p1 - 3)
            ElseIf p1 > 1 Then
                'Remove from middle
                OldValue = Left(OldValue, p1 - 1) & Mid(OldValue, p2 - 2)
            End If
            Target.Value = OldValue
        End If
    End If
   
    Application.EnableEvents = True

End Sub
Notice that both InStr arguments are surrounded with vbNewLine characters to ensure that the correct value is found when deselecting a value. The p1 and p2 arguments in the subsequent Mid and Left functions account for the extra vbNewLines. Without this, if the dropdown list contained Bobby and Bob in that order and you select Bob to deselect it then Bobby would be removed instead.
Hi John,

Thank you so much for this answer. This solves my question beautifully. You have saved me from many frustrating hours!
 
Upvote 0

Forum statistics

Threads
1,215,036
Messages
6,122,796
Members
449,095
Latest member
m_smith_solihull

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top