Multiple Selection Code

sarahrb1989

New Member
Joined
Dec 15, 2017
Messages
30
I don't know very much about coding or the Excel developer tool. :eeek: I have a spreadsheet for work that has demographic information that needs to be input. Currently, I have a combo box using data validation lists that does this for me. Here is the code I currently have in place which changes my selection to a "code". For Example, I choose Male and it changes to M.


Code:
Option Explicit
' Developed by Contextures Inc.
' [URL="http://www.contextures.com"]www.contextures.com[/URL]
Private Sub Combobox2_KeyDown(ByVal _
        KeyCode As MSForms.ReturnInteger, _
        ByVal Shift As Integer)
    'Hide combo box and move to next cell on Enter and Tab
    Select Case KeyCode
        Case 9
            ActiveCell.Offset(0, 1).Activate
        Case 13
            ActiveCell.Offset(1, 0).Activate
        Case Else
            'do nothing
    End Select
End Sub
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim str As String
Dim cboTemp As OLEObject
Dim ws As Worksheet
Dim wsList As Worksheet
Set ws = ActiveSheet
Set wsList = Sheets("Demographics Options")
Cancel = True
Set cboTemp = ws.OLEObjects("Combobox2")
  On Error Resume Next
  With cboTemp
    .ListFillRange = ""
    .LinkedCell = ""
    .Visible = False
  End With
On Error GoTo errHandler
  If Target.Validation.Type = 3 Then
    Application.EnableEvents = False
    str = Target.Validation.Formula1
    str = Right(str, Len(str) - 1)
    With cboTemp
      .Visible = True
      .Left = Target.Left
      .Top = Target.Top
      .Width = Target.Width + 15
      .Height = Target.Height + 5
      .ListFillRange = str & "_Codes"
      .LinkedCell = Target.Address
    End With
    cboTemp.Activate
  End If
  
errHandler:
  Application.EnableEvents = True
  Exit Sub
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim str As String
Dim cboTemp As OLEObject
Dim ws As Worksheet
Set ws = ActiveSheet
Application.EnableEvents = False
Application.ScreenUpdating = False
If Application.CutCopyMode Then
  'allows copying and pasting on the worksheet
  GoTo errHandler
End If
Set cboTemp = ws.OLEObjects("ComboBox2")
  On Error Resume Next
  With cboTemp
    .Top = 10
    .Left = 10
    .Width = 0
    .ListFillRange = ""
    .LinkedCell = ""
    .Visible = False
    .Object.Value = ""
  End With
errHandler:
  Application.ScreenUpdating = True
  Application.EnableEvents = True
  Exit Sub
End Sub

I have one column in the spreadsheet that requires the option of selecting multiple items from the drop down menu and putting them in the same cell together separated by a comma. I have a few different options of code from the same website that do something similar. However, none of these change the item selected to a "code" version.

For example, I choose African American and Caucasian. I would like the cell to read AA, Cauc.

Is there a way to alter any of these codes (including the above) to do this?
:confused:

Here is the sample code for the other options:

http://blog.contextures.com/archives/2014/01/21/multiple-selection-drop-down-with-codes/

Code:
Option Explicit
' Developed by Contextures Inc.
' [URL="http://www.contextures.com"]www.contextures.com[/URL]
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
Dim lCode As Long
Dim wsList As Worksheet
Dim rngList As Range
Dim rngListID As Range
If Target.Count > 1 Then GoTo exitHandler
Set wsList = ActiveSheet
Set rngList = wsList.Range("NumWordList")
Set rngListID = wsList.Range("NumWordID")
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
  Application.Undo
  oldVal = Target.Value
  Target.Value = newVal
  If Target.Column = 3 Then
    If oldVal = "" Then
      'do nothing
         lCode = rngListID.Range("A1") _
            .Offset(Application. _
                WorksheetFunction _
                .Match(Target.Value, _
                rngList, 0) - 1, 0)
         Target.Offset(0, 1).Value = lCode
   
   Else
      If newVal = "" Then
        'do nothing
         Target.Offset(0, 1).ClearContents
      Else
         lCode = rngListID.Range("A1") _
            .Offset(Application. _
                WorksheetFunction _
                .Match(Target.Value, _
                rngList, 0) - 1, 0)
         Target.Value = oldVal _
           & ", " & newVal
         Target.Offset(0, 1).Value = Target.Offset(0, 1).Value _
           & ", " & lCode
      End If
    End If
  End If
End If
exitHandler:
  Application.EnableEvents = True
End Sub

http://www.contextures.com/excel-data-validation-multiple.html

Code:
Option Explicit
' Developed by Contextures Inc.
' [URL="http://www.contextures.com"]www.contextures.com[/URL]
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
Dim lType As Long
Dim strList As String
Application.EnableEvents = False
On Error Resume Next
   lType = Target.Validation.Type
On Error GoTo exitHandler
If lType = 3 Then
  'if the cell contains a data validation list
   Cancel = True
   strList = Target.Validation.Formula1
   strList = Right(strList, Len(strList) - 1)
   strDVList = strList
   frmDVList.Show
End If
exitHandler:
  Application.EnableEvents = True
 
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
Dim strSep As String
strSep = ", "
  Application.EnableEvents = False
On Error Resume Next
If Target.Count > 1 Then GoTo exitHandler
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
  newVal = Target.Value
  Application.Undo
  oldVal = Target.Value
  Target.Value = newVal
   If newVal = "" Then
      'do nothing
   Else
         If oldVal = "" Then
            Target.Value = newVal
         Else
            Target.Value = oldVal & strSep & newVal
         End If
    End If
End If
exitHandler:
  Application.EnableEvents = True
End Sub

:warning: Please let me know if any additional information is needed. My preference would be to alter the first code for the specific column I need to allow for multiple selections in the same cell that use a "code" version of the selection.
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Try this returned File:-
https://app.box.com/s/t01p6oi1yuy4mx8pqx6d3vtz0cknpum6

NB:- In your file there are two other event codes relating to comboboxes that appear to be hidden in "A1". I don't know what they are for, the purpose is not apparent !!
For the Purpose of getting my code to work I have "Remarked Out" the other "Event" codes.
If you wish to run the "Event" code I've entered in the sheet Module together with your other "Event" codes it would perhaps be best to run them all from the same "Event" code, by Qualifying the code, so that each event is related to a specific cell, Like:-
(See my code for example and below)
[
CODE]
If Target.address(0,0)= "M1" then
'Run my code here
end if
[/Code]

Regrds Mick
 
Upvote 0
Thank you, I do need to run all three event codes. The two previous event codes were to use a Combo box that pulls from the data validation list and changes to an abbreviation. If you double click inside the spreadsheet you will see what I mean. I need this to run in Columns C-J.

Can you alter the code so that it can run both? I would like your code to run in column M only and the other two event codes to run in column C-J on the first sheet.

Do you know if there is a way to alter the two event codes I already have and allow for multiple selections in Column M? Please let me know!

Thank you!
 
Last edited:
Upvote 0
Please try the returned code with all macros intact and hopefully working.
Code:
Option Explicit
'[COLOR="Green"][B] Developed by Contextures Inc.[/B][/COLOR]
'[COLOR="Green"][B] [URL="http://www.contextures.com"]www.contextures.com[/URL][/B][/COLOR]
Private [COLOR="Navy"]Sub[/COLOR] Combobox2_KeyDown(ByVal _
        KeyCode [COLOR="Navy"]As[/COLOR] MSForms.ReturnInteger, _
        ByVal Shift [COLOR="Navy"]As[/COLOR] Integer)
    '[COLOR="Green"][B]Hide combo box??? and move to next cell on Enter and Tab[/B][/COLOR]
  MsgBox KeyCode
    [COLOR="Navy"]Select[/COLOR] [COLOR="Navy"]Case[/COLOR] KeyCode
        Case 9 '[COLOR="Green"][B]Tab[/B][/COLOR]
            ActiveCell.Offset(0, 1).Activate
        Case 13 '[COLOR="Green"][B]Enter[/B][/COLOR]
            ActiveCell.Offset(1, 0).Activate
        [COLOR="Navy"]Case[/COLOR] [COLOR="Navy"]Else[/COLOR]
            '[COLOR="Green"][B]do nothing[/B][/COLOR]
    [COLOR="Navy"]End[/COLOR] Select
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Private [COLOR="Navy"]Sub[/COLOR] Worksheet_Change(ByVal Target [COLOR="Navy"]As[/COLOR] Range)
[COLOR="Navy"]Dim[/COLOR] rngDV [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] oldVal [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Dim[/COLOR] newVal [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Dn [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]On[/COLOR] [COLOR="Navy"]Error[/COLOR] [COLOR="Navy"]Resume[/COLOR] [COLOR="Navy"]Next[/COLOR]
With Sheets("Demographics Options") '[COLOR="Green"][B]Change Date sheet here as required[/B][/COLOR]
        [COLOR="Navy"]Set[/COLOR] Rng = .Range("R2", .Range("R" & Rows.Count).End(xlUp))
    [COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]If[/COLOR] Target.Count = 1 And Target.Address(0, 0) = "M1" [COLOR="Navy"]Then[/COLOR]
   Application.EnableEvents = False
          [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
                [COLOR="Navy"]If[/COLOR] Dn.Value = Target.Value [COLOR="Navy"]Then[/COLOR]
                    newVal = Dn.Offset(, -1).Value
                    [COLOR="Navy"]Exit[/COLOR] For
                [COLOR="Navy"]End[/COLOR] If
           [COLOR="Navy"]Next[/COLOR] Dn
            Application.Undo
            oldVal = Target.Value
            Target.Value = newVal
                [COLOR="Navy"]If[/COLOR] Not newVal = "" [COLOR="Navy"]Then[/COLOR]
                    Target.Value = IIf(oldVal = "", newVal, oldVal & ", " & newVal)
                [COLOR="Navy"]End[/COLOR] If
    Application.EnableEvents = True
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Private [COLOR="Navy"]Sub[/COLOR] Worksheet_BeforeDoubleClick(ByVal Target [COLOR="Navy"]As[/COLOR] Range, Cancel [COLOR="Navy"]As[/COLOR] Boolean)
[COLOR="Navy"]Dim[/COLOR] str [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Dim[/COLOR] cboTemp [COLOR="Navy"]As[/COLOR] OLEObject
[COLOR="Navy"]Dim[/COLOR] ws [COLOR="Navy"]As[/COLOR] Worksheet
[COLOR="Navy"]Dim[/COLOR] wsList [COLOR="Navy"]As[/COLOR] Worksheet
[COLOR="Navy"]Set[/COLOR] ws = ActiveSheet
[COLOR="Navy"]Set[/COLOR] wsList = Sheets("Demographics Options")
Cancel = True
[COLOR="Navy"]Set[/COLOR] cboTemp = ws.OLEObjects("Combobox2")
  [COLOR="Navy"]On[/COLOR] [COLOR="Navy"]Error[/COLOR] [COLOR="Navy"]Resume[/COLOR] [COLOR="Navy"]Next[/COLOR]
  [COLOR="Navy"]With[/COLOR] cboTemp
    .ListFillRange = ""
    .LinkedCell = ""
    .Visible = False
  [COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]On[/COLOR] [COLOR="Navy"]Error[/COLOR] GoTo errHandler
  [COLOR="Navy"]If[/COLOR] Target.Validation.Type = 3 [COLOR="Navy"]Then[/COLOR]
    Application.EnableEvents = False
    str = Target.Validation.Formula1
    str = Right(str, Len(str) - 1)
    [COLOR="Navy"]With[/COLOR] cboTemp
      .Visible = True
      .Left = Target.Left
      .Top = Target.Top
      .Width = Target.Width + 15
      .Height = Target.Height + 5
      .ListFillRange = str & "_Codes"
      .LinkedCell = Target.Address
    [COLOR="Navy"]End[/COLOR] With
    cboTemp.Activate
  [COLOR="Navy"]End[/COLOR] If
errHandler:
  Application.EnableEvents = True
  [COLOR="Navy"]Exit[/COLOR] [COLOR="Navy"]Sub[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Private [COLOR="Navy"]Sub[/COLOR] Worksheet_SelectionChange(ByVal Target [COLOR="Navy"]As[/COLOR] Range)
[COLOR="Navy"]Dim[/COLOR] str [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Dim[/COLOR] cboTemp [COLOR="Navy"]As[/COLOR] OLEObject
[COLOR="Navy"]Dim[/COLOR] ws [COLOR="Navy"]As[/COLOR] Worksheet
[COLOR="Navy"]Set[/COLOR] ws = ActiveSheet
Application.EnableEvents = False
Application.ScreenUpdating = False
[COLOR="Navy"]If[/COLOR] Application.CutCopyMode [COLOR="Navy"]Then[/COLOR]
  '[COLOR="Green"][B]allows copying and pasting on the worksheet[/B][/COLOR]
  GoTo errHandler
[COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Set[/COLOR] cboTemp = ws.OLEObjects("ComboBox2")
  [COLOR="Navy"]On[/COLOR] [COLOR="Navy"]Error[/COLOR] [COLOR="Navy"]Resume[/COLOR] [COLOR="Navy"]Next[/COLOR]
  [COLOR="Navy"]With[/COLOR] cboTemp
    .Top = 10
    .Left = 10
    .Width = 0
    .ListFillRange = ""
    .LinkedCell = ""
    .Visible = False
    .Object.Value = ""
  [COLOR="Navy"]End[/COLOR] With
errHandler:
  Application.ScreenUpdating = True
  Application.EnableEvents = True
  [COLOR="Navy"]Exit[/COLOR] [COLOR="Navy"]Sub[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
It still isn't working. When I put the code in, and used the combo box and pressed enter it did change to the abbreviated version like it is supposed to but gave me a popup that said "13". And I couldn't choose multiple selections in column R.
 
Upvote 0
I mean column M on the first sheet.

The original code I have is already pulling from the data validation list and using the abbreviated version. The only thing left I need is to allow for multiple selections in column M on the first sheet. I hope this makes sense.

It still isn't working. When I put the code in, and used the combo box and pressed enter it did change to the abbreviated version like it is supposed to but gave me a popup that said "13". And I couldn't choose multiple selections in column R.
 
Upvote 0
That makes sense. Thank you for sending the file. I need that code to work for the entire M column not just the M1 cell. Is that possible? Please let me know.
 
Upvote 0
Try this for all of Colum "M".
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
Dim Rng As Range
Dim Dn As Range
On Error Resume Next
With Sheets("Demographics Options") 'Change Date sheet here as required
        Set Rng = .Range("R2", .Range("R" & Rows.Count).End(xlUp))
    End With
If Target.Count = 1 Then
  If Not Intersect(Target, Range("M:M")) Is Nothing Then
     Application.EnableEvents = False
           For Each Dn In Rng
                If Dn.Value = Target.Value Then
                    newVal = Dn.Offset(, -1).Value
                    Exit For
                End If
           Next Dn
            Application.Undo
            oldVal = Target.Value
            Target.Value = newVal
                If Not newVal = "" Then
                    Target.Value = IIf(oldVal = "", newVal, oldVal & ", " & newVal)
                End If
    Application.EnableEvents = True
    End If
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,304
Members
452,633
Latest member
DougMo

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