Multiple selections(add,edit,remove)

NekoNovi

New Member
Joined
May 26, 2016
Messages
1
Hello everyone,

I am trying to select multiple items from drop down list and to add values to the same cell(separated by comma).
Something like described here(Add Values To the Same Cell section): http://www.contextures.com/excel-dat...-multiple.html

Because I don't have experience with excel or visual basic it's hard to understand. On posted page I found sample file where you can add and remove items only from drop down list(keyboard option is not allowed).
From some reason this doesn't work for me(keyboard option is allowed). I spent lot of time on debugging this, but unsuccessfully. Can anyone help me with this?

Code:
Code:
[/I]Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
Dim strVal As String
Dim i As Long
Dim lCount As Long
Dim Ar As Variant
On Error Resume Next
Dim lType As Long
If Target.Count > 1 Then GoTo exitHandler


lType = Target.Validation.Type
If lType = 3 Then
    Application.EnableEvents = False
    newVal = Target.Value
    Application.Undo
    oldVal = Target.Value
    Target.Value = newVal
    If Target.Column = 3 Then
        If oldVal = "" Then
            'do nothing
        Else
            If newVal = "" Then
                'do nothing
            Else
                On Error Resume Next
                Ar = Split(oldVal, ", ")
                strVal = ""
                For i = LBound(Ar) To UBound(Ar)
                    Debug.Print strVal
                    Debug.Print CStr(Ar(i))
                    If newVal = CStr(Ar(i)) Then
                        'do not include this item
                        strVal = strVal
                        lCount = 1
                    Else
                        strVal = strVal & CStr(Ar(i)) & ", "
                    End If
                Next i
                If lCount > 0 Then
                    Target.Value = Left(strVal, Len(strVal) - 2)
                Else
                    Target.Value = strVal & newVal
                End If
            End If
        End If
    End If
End If


exitHandler:
  Application.EnableEvents = True
End Sub




[I]

If I understand just this line of code is changeable(you pass desired column): If Target.Column = 4 Then ?

Also is there any way to expand this code to work next things:
1) You can add and remove items from drop down list
2) You can add, remove and edit items from keyboard but if I enter some value which is not valid error message is show?

Because I can't figure how to do step 2 correctly - there are some strange behaviour(if I remove some item excel duplicates items in column or if I enter some value I can't change it), I tried to run this code example where keyboard changes are disabled.

I will really appreciate any help, and sorry for bad English.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

Forum statistics

Threads
1,214,983
Messages
6,122,582
Members
449,089
Latest member
Motoracer88

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