Click here to download the featured file. This link will take you to our sister site. (Open the workbook, ALT F11 to open the VBE and the code can be found in the relevant sheet object)
Excel now blocks macros by default when you download a workbook from the internet. Therefore none of these macros will run in the workbook I have provided unless you unblock the macros. Please see this page for guidance https://learn.microsoft.com/en-us/deployoffice/security/internet-macros-blocked
Step 1: Create a List of Values and Name It
In the video I create a list of values that I wanted to appear in the drop-down list on a separate worksheet. I also name the list.
To name a list:
- Select the cells in the list
- Click in the Name Box (just to the left of the formula bar – it displays the cell address of the active cell by default)
- Type a name that starts with a letter and does not contain spaces
- Press the Enter key on your keyboard to confirm
Step 2: Create the Drop-down List Using Data Validation
The next step is to create the drop-down list. I did this on a separate worksheet to the list created above in step 1, although it could be created on the same worksheet. To create the drop-down list:
- Select the cell or cells you want the drop-down list to appear in
- Click on the Data tab on Excel’s ribbon
- Click on the Data Validation button in the Data Tools group
- In the Data Validation dialog, in the Allow: list select List
- Click in the Source: box
- Press F3 on your keyboard to open the Paste Name dialog
- Select the name you gave your list in Step 1
- Click on OK
- Click OK in the Data Validation dialog
By default a drop-down list will only allow you to select one item.
STEP3: Add the VBA Code to Your Drop-Down List
To add the VBA (visual basic for applications) code to your drop-down list follow these steps:
- Use the keyboard shortcut ALT F11 to open the Visual Basic Editor (VBE)
- Make sure the Project Explorer is visible – it displays all the workbook’s worksheet names. If you can’t see it, use the keyboard shortcut CTRL R to open it.
- In the Project Explorer select the worksheet that contains your drop-down list.
- In the Code window (that’s the big white area to the right of the Project Explorer), paste the code included in the downloadable workbook (see form above to download the workbook).
- Close the VBE and you should find that you can select multiple items in your drop-down list.
There are a few tweaks to the code that you make and I go through these in the video. One thing you should note is that the code, as it is, assumes you drop-down is in cell A2. If it isn’t, change the range address in line 6.
Please note, when you save the workbook, because it now contains VBA code, it needs to be saved as a Macro-enabled Workbook (*.xlsm).
New line 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("A2")) 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
Comma-separated 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("A2")) 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 & ", " & Newvalue Else: Target.Value = Oldvalue End If End If End If End If Application.EnableEvents = True Exitsub: Application.EnableEvents = True End Sub
Range of cells 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("A2:C2")) 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 & ", " & Newvalue Else: Target.Value = Oldvalue End If End If End If End If Application.EnableEvents = True Exitsub: Application.EnableEvents = True End Sub
Whole column 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("B:B")) 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 & ", " & Newvalue Else: Target.Value = Oldvalue End If End If End If End If Application.EnableEvents = True Exitsub: Application.EnableEvents = True End Sub
Without drop-down 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("B:B")) Is Nothing Then 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 & ", " & Newvalue Else: Target.Value = Oldvalue End If End If End If Application.EnableEvents = True Exitsub: Application.EnableEvents = True End Sub
Multi drop-down 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("A2")) 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