Home > Macros > Basics > Combo Boxes Excel Worksheet Combo Box MacrosClick a button on the worksheet, and a macro runs, to select the next item in the Combobox list. It's quicker than clicking the arrow, and selecting from the drop down list! Or, run macros to list all the combo buttons on a worksheet, with details about each combo box, for workbook troubleshooting. Author: Debra Dalgleish |
Excel Combo Box TypesWhen I'm making drop down lists in Excel, most of the time I use the data validation feature. That creates an in-cell drop down, like the list of months in the screen shot below. Sometimes, if I want a larger font, or more visible items in the drop down, I use a Combo Box instead. In the screen shot below, you can see a data validation cell, and below that there are two types of Combo Boxes:
In the sections below, there are macros for those two types of Combo Boxes -- 1) ActiveX Combo Box Macros -- 2) Form Control Combo Box Macros |
Video: Compare 3 Excel Drop Down List TypesThis short video shows the 3 types of Excel drop down lists, and the steps for setting up the ActiveX combo box, and the Form Control combo box. There are written steps on the Drop Down Lists for Data Entry page. Video Timeline
|
1) ActiveX Combo Box MacrosSeveral of my Excel sample files use ActiveX comboboxes with data validation. Click on a cell that has a drop down list, and a combobox appears, showing the items from the selected cell’s drop down. For example, click here to see the combobox example that works with named ranges. There are two Activex Combo Box macros in the sections below. -- a) List all Activex Combo Boxes on active sheet -- b) Scroll through Combo Box list items |
List All ActiveX Combo BoxesThis macro lists all of the ActiveX combo boxes on the active worksheet, with details about each combo box. If there are other types of ActiveX controls on the worksheet, those are not included in the list. In the sample workbook, there is a "List Combos" button on the worksheet, and it runs the macro named ListAllCombosAX (the VBA code is shown in the next section) Combo Box Details ListThe screen shot below shows the first 4 columns in the list that the ListAllCombosAX macro creates, on a new sheet in your workbook. In the sample file, there are 3 combo boxes on the worksheet, and you can see the details for each combo box in the list. |
List Combo Boxes - Excel VBA CodeCopy this macro code to the workbook where you want to create a list of ActiveX combo boxes. Paste the code into a regular code module. Sub ListAllCombosAX() |
Scroll Through ActiveX Combo Box ItemsIn the sample workbook, there is a combo box that shows a list of weekday names. The combo box is linked to cell C7, where the selected weekday name appears. In cells C9 and C11, SUMIF formulas calculate the totals for the selected day, based on the entries in the Sales Orders table. The "Next Day" button runs a macro, named ChangeComboValue, that selects the next day, in the combo box. This button makes it easy to quickly scroll through the list of weekdays, to see each weekday's totals. This screen shot shows the first four columns in the Sales Order table. In column B, the following formula calculates the weekday name:
See more TEXT function examples on the Combine Cells in Excel page. |
Scroll Combo Box Items - Excel VBA CodeCopy this macro code to the workbook where you want to scroll through the items in aForm Control combo box, then paste the code into a regular code module. Modify the CodeIn the code, there are references to the sheet name (ComboBox), and the combo box name (ComboDay).
Sub ChangeComboValue() Dim cboDay As OLEObject Dim wsCB As Worksheet Dim lListCount As Long Dim lListGo As Long Set wsCB = Sheets("ComboBox") Set cboDay _ = wsCB.OLEObjects("ComboDay") With cboDay.Object lListCount = .ListCount If .ListIndex _ = .ListCount - 1 Then lListGo = 0 Else lListGo = .ListIndex + 1 End If .Value = .List(lListGo) End With End Sub |
2) Form Control Combo Box MacrosInstead of using data validation, or an ActiveX combo box, you could use a Form Control Combo Box. You can't change its font or font size, but you can increase the number of visible rows.
There are two Form Control Combo Box macros in the sections below. -- a) List all Form Control Combo Boxes on active sheet -- b) Scroll through Combo Box list items |
a) List All Form Control Combo BoxesThis macro lists all of the form control combo boxes on the active worksheet, with details about each combo box. In the sample workbook, there is a "List Combos" button on the worksheet, and it runs the macro named ListAllDropDowns (VBA code is shown in the next section) This screen shot shows the first 5 columns in the list that the ListAllDropDowns macro creates, on a new sheet in your workbook. In the sample file, there are 3 combo boxes on the worksheet, and you can see the details for each combo box in the list.. |
List Combo Boxes - Excel VBA CodeCopy this macro code to the workbook where you want to create a list of Form Control combo boxes. Paste the code into a regular code module. Sub ListAllDropDowns() 'code for form control combo box ''called DropDowns in VBA Dim wsList As Worksheet Dim wsA As Worksheet Dim dd As DropDown Dim ddList As ListObject Dim lRow As Long Dim ldds As Long Dim lNum As Long Dim lCount As Long Dim LastCol As Long Dim strdd As String Dim nm As Name Dim nmRng As Range Dim nmSh As String Dim strSel As String On Error Resume Next Set wsA = ActiveSheet lRow = 1 LastCol = 8 ldds = wsA.DropDowns.Count If ldds = 0 Then MsgBox "No Form Control combo box on sheet" Exit Sub End If Set wsList = Sheets.Add With wsList .Range(.Cells(lRow, 1), _ .Cells(lRow, LastCol)).Value _ = Array("Name", "Index", "Linked Cell", _ "TopLeft", "List", "List Sht", _ "Item", "Sel") lRow = lRow + 1 For Each dd In wsA.DropDowns lCount = lCount + 1 If lCount > ldds Then Exit For strdd = dd.Name Set dd = wsA.DropDowns(strdd) Set nm = ThisWorkbook.Names(dd.ListFillRange) Set nmRng = nm.RefersToRange nmSh = nm.RefersToRange.Parent.Name lNum = dd.ListIndex If lNum = 0 Then strSel = "No Selection" Else strSel = nmRng.Cells(dd.ListIndex, 1).Value End If If Not dd Is Nothing Then .Range(.Cells(lRow, 1), _ .Cells(lRow, LastCol)).Value _ = Array(dd.Name, dd.Index, dd.LinkedCell, _ dd.TopLeftCell.Address, _ dd.ListFillRange, nmSh, _ dd.ListIndex, strSel) lRow = lRow + 1 End If Next dd 'change list to Excel table Set ddList = .ListObjects.Add(xlSrcRange, _ .Range("A1").CurrentRegion, , xlYes) ddList.TableStyle = "TableStyleLight8" ddList.DataBodyRange.CurrentRegion.Columns.AutoFit End With End Sub |
b) Scroll Through Combo Box ItemsIn the sample workbook, there is a combo box that shows a list of weekday names. The combo box is linked to cell C7, where a number appears. In cells C9 and C11, SUMIF formulas calculate the totals for the selected day, based on the entries in the Sales Orders table. The "Next Day" button runs a macro that selects the next day, in the combo box. This makes it easy to quickly scroll through the weekdays, to see the weekday totals. This screen shot shows the first four columns in the Sales Order table. In column B, the following formula calculates the weekday number:
See more date functions on the Excel Date Function Examples page. |
Scroll Combo Box Items - Excel VBA CodeCopy this macro code to the workbook where you want to scroll through the items in aForm Control combo box, then paste the code into a regular code module. Modify the CodeIn the code, there are references to the sheet name (ComboFC), and the combo box name (DaysDD).
Sub ChangeComboValueFC() 'code for form control combo box 'selects next item in list ' after last item, goes to top of list Dim ddTest As DropDown Dim wsCB As Worksheet Dim lListCount As Long Dim lListGo As Long Set wsCB = Sheets("ComboFC") Set ddTest = wsCB.DropDowns("DaysDD") With ddTest lListCount = .ListCount If .ListIndex = .ListCount Then lListGo = 1 Else lListGo = .ListIndex + 1 End If .ListIndex = lListGo End With End Sub |
Download Sample Files
|
Data Validation with Combo Box
Last updated: May 10, 2023 3:32 PM