Practice WorkbookYou are welcome to download the practice workbook from the link below. Show
Make Multiple Selection from Drop Down List.xlsm Multiple Selection in a Drop-Down ListFirst of all, we need to create a drop-down list on the basis of our stationeries. Let’s create it quickly. Don’t hesitate to visit the article regarding the making of a drop-down list. In the Data Validation dialog box select the LIST data type and insert the cell range of the items. B4:B11 is the range that holds the stationery elements. Now you will find the drop-down list. Read more: Excel Drop Down List Depending on Selection 1. Select Multiple Items (Allows Duplicate Selection)A conventional drop-down list always selects a single item. Here you can see, we have selected Pen from the list (image below). Now, if we select another item, let’s say Pencil then it will replace the previous value. Only Pencil will remain selected. To select multiple items, we need to use the VBA code. Open the Microsoft Visual Basic for Applications window (press ALT + F11 to open it). Now double click on the worksheet name or number where you want to select multiple items within the drop-down list. You will find the code window for that particular sheet. Here, is the code window for Sheet2 in our workbook (we have the drop-down list in this sheet). Once the code window is opened, insert the following code there Private Sub Worksheet_Change(ByVal Target As Range) Dim Oldvalue As String Dim Newvalue As String On Error GoTo Exitsub If Target.Address = "$D$4" 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 Target.Value = Oldvalue & ", " & Newvalue End If End If End If Application.EnableEvents = True Exitsub: Application.EnableEvents = True End SubSave the code, and now try to select values in the drop-down list. Having selected Pencil, we are going to select another item Notebook. And you can see, we found both the items (image below). This code will allow us to repeat the selection. Let’s say if we select Pencil again, we will find the item again in the selection box. Code ExplanationWe have declared two strings Oldvalue and Newvalue. You can see we made the drop-down list in the D4 cell, that’s why our target address is D4. And in addition, we have rechecked whether the cell is using data validation or not using Target.SpecialCells. Once a value is selected, we turned off events (Application.EnableEvents = False) so changes don’t trigger the event again. Then stored the selected item into the Newvalue. After undoing the change, we have set the value into the Oldvalue. Then check whether the Oldvalue is empty or not. If empty (means only one value is selected), then return the Newvalue. Otherwise, concatenate the Oldvalue and Newvalue. Before ending the reset the event, so that we can change if required. Read more: Multiple Dependent Drop-Down List Excel VBA 2. Select Multiple Items From Drop Down List (Unique Selection Only)In the earlier section, we have seen the multiple selections where repetition was allowed. If you don’t want that, then follow this section. For convenience, we used a separate sheet for this demonstration. This time we are at Sheet3. Write the following code in the code window for this sheet. Private Sub Worksheet_Change(ByVal Target As Range) Dim Oldvalue As String Dim Newvalue As String Application.EnableEvents = True On Error GoTo Exitsub If Target.Address = "$D$4" 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 SubIs there any difference compared to the earlier code! Have a closer look, you will be able to spot the slight difference. Here we have used a VBA function called INSTR. The INSTR function returns the position of the first occurrence of a substring in a string. Visit this INSTR article for further information. Using this logical operation with InStr(1, Oldvalue, Newvalue) = 0, we have checked whether the values are found or not. If the logical operation returns TRUE (not found earlier) then it allows to select the item and concatenate with the earlier value. Save the code and now try to select an item that has already been selected. Here we have already selected Pencil, if we want to select that again, we can’t. It doesn’t allow duplicate values. Read more: How to Create a Drop Down List From Another Sheet in Excel 3. Select Items in NewlineSo far, we have found the items are separated by a comma. In this section, we will arrange the selected items in newlines. For simplicity, we are merging a few cells with the D4 cell. To do that, select the cells you want to merge and click Merge & Center from the Alignment section of the Home tab. The cell will gain more height. Now, let’s look at the code for separating items through newline. Use the following 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 Target.Address = "$D$4" 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 SubThe only difference from the previous code is that this time we used vbNewLine in between OldValue and NewValue. vbNewLine provides a new line between the items. Now select the items. We select an item Pen that is showing in the image above. Now select another element. You will find the two items are in different lines. Here we have two values, that are in two different lines. Selecting another value will add that to another line. Every value will be in a new line. Note that if you want another delimiter to separate the items, use that within double quotes in place of vbNewline. Download Practice WorkbookHow to Create Drop Down List in Excel with Multiple Selections.xlsm Steps to Create Drop-Down List in Excel with Multiple SelectionsHere we’ve got a data set with the Names of some books in a bookshop called Martin Bookstore. Our objective today is to create a drop-down list based on this data set that takes multiple selections. We will accomplish this in two steps. Step 1: Creating a Drop-Down List by Data Validation Step 2: Enabling that List to Accept Multiple Selections by VBA Code Step 1: Creating a Drop-Down List by Data Validation
Similar Readings:
Step 2: Enabling the Drop-Down List to Accept Multiple Selection by VBA CodeNow we’ve created a drop-down list. But up till now, it accepts a single selection. We want it to accept multiple selections.
Case 1: VBA Code for Multiple Selection with RepetitionIf you want multiple selections with repetition, enter the following code: Code: Private Sub Worksheet_Change(ByVal Target As Range) Dim Oldvalue As String Dim Newvalue As String On Error GoTo Exitsub If Target.Address = "$D$4" 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 Target.Value = Oldvalue & ", " & Newvalue End If End If End If Application.EnableEvents = True Exitsub: Application.EnableEvents = True End SubNote: Here in the 5th line of the code (If Target.Address = "$D$4" Then), “$D$4” is the cell where I am entering the list. You use your one. If you want to enter the list in multiple cells, then repeat the line for multiple cells using an or operator. For example, if you want to enter the list in cells D4, F4, and H4, use: If Target.Address = "$D$4" or Target.Address = "$F$4" or Target.Address = "$H$4" Then Case 2: VBA Code for Multiple Selection without RepetitionAnd if you don’t want repetition, use this code instead. 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 Target.Address = "$D$4" 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 SubNote: Here in the 6th line of the code (If Target.Address = "$D$4" Then), “$D$4” is the cell where I am entering the list. You use your one. If you want to enter the list in multiple cells, then repeat the line for multiple cells using an or operator. For example, if you want to enter the list in cells D4, F4, and H4, use: If Target.Address = "$D$4" or Target.Address = "$F$4" or Target.Address = "$H$4" Then After entering any of the above codes, save the file as an Excel Macro-Enabled Workbook (*.xlsm). Now come back to your worksheet and go to the drop-down list you created earlier. You will be able to make multiple selections from the list now, separated by commas (,). Excel Data Validation - Select Multiple ItemsInstead of limiting a data validation drop down list to a single selection, you can use this macro to allow multiple selections. Video and written instructions, and get the free workbook to see how it works.
Select Multiple Items from Popup ListboxShow a listbox, when a cell with an Excel drop down list is selected. Select multiple items from the list, and all selected items are entered in the active cell. There is also a sample file for single item selection. Select Multiple Items From Drop Down List Choose Items from Listbox Macro to Select Multiple Items UserForm With Listbox Get the Sample Files More Tutorials Preparation
The code should be in the worksheet’s editor containing the dropdown, because the code tracks changes in the cell. How to create drop down list with multiple selections or values in Excel?By default, you can only select one item per time from a data validation drop-down list in Excel. How to make multiple selections from the drop-down list as below screenshot shown? The methods in this article can help you solve the problem. Create drop down list with multiple selections with VBA code Create drop down list with multiple selections with VBA codeYou can apply the below VBA code to make multiple selections from the drop-down list in a worksheet in Excel. Please do as follows. 1. Open the worksheet you have set data validation drop-down list, right click on the sheet tab and select View Code from the context menu. 2. In the Microsoft Visual Basic for Applications window, copy the below VBA code into the code window. See screenshot: VBA code: drop down list with multiple selections Private Sub Worksheet_Change(ByVal Target As Range) 'Updated by Extendoffice 2019/11/13 Dim xRng As Range Dim xValue1 As String Dim xValue2 As String If Target.Count > 1 Then Exit Sub On Error Resume Next Set xRng = Cells.SpecialCells(xlCellTypeAllValidation) If xRng Is Nothing Then Exit Sub Application.EnableEvents = False If Not Application.Intersect(Target, xRng) Is Nothing Then xValue2 = Target.Value Application.Undo xValue1 = Target.Value Target.Value = xValue2 If xValue1 <> "" Then If xValue2 <> "" Then If xValue1 = xValue2 Or _ InStr(1, xValue1, ", " & xValue2) Or _ InStr(1, xValue1, xValue2 & ",") Then Target.Value = xValue1 Else Target.Value = xValue1 & ", " & xValue2 End If End If End If End If Application.EnableEvents = True End Sub3. Press the Alt + Q keys to close the Microsoft Visual Basic for Applications window. Now you can select multiple items from the drop-down list in current worksheet. Notes:
Easily create drop down list with multiple selections with an amazing toolHere highly recommended the Multi-select Drop-down List feature of Kutools for Excel for you. With this feature, you can easily select multiple items from the drop-down list in a specified range, current worksheet, current workbook or all opened workbooks as you need. Before applying Kutools for Excel, please download and install it firstly. 1. Click Kutools > Drop-down List > Multi-select Drop-down List > Settings. See screenshot: 2. In the Multi-select Drop-down List Settings dialog box, please configure as follows.
3. Please click Kutools > Drop-down List > Multi-select Drop-down List to enable the feature. Now you can select multiple items from the drop-down list in current worksheet or any scope you have specified in step 2. If you want to have a free trial ( 30-day) of this utility, please click to download it, and then go to apply the operation according above steps. Related articles:Autocomplete when typing in Excel drop down list Create drop down list from another workbook in Excel Create a searchable drop down list in Excel Auto populate other cells when selecting values in Excel drop down list More tutorial for drop down list... The Best Office Productivity ToolsKutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%
Read More... Free Download... Purchase... Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier
Read More... Free Download... Purchase...
Say something here... symbols left. You are guest
or post as a guest, but your post won't be published automatically. Post Loading comment... The comment will be refreshed after 00:00.
|