Contextures

Data Validation Combo box

To overcome the limitations of a data validation drop down list, you can add a combo box to your worksheet, and use programming to make it appear in cells that contain a data validation list

NOTE: You can't use ActiveX controls on a Macintosh. Prof. Lee Townsend shows how to create drop downs using Form control combo boxes.

Introduction

You can use Data Validation to create a dropdown list of options in a cell. However, the list font can't be changed, nor can the number of visible rows, which has a maximum of eight. Also, Data Validation doesn't have an AutoComplete feature, which finds matching items in the list as you start to type.

dropdown list of options in a cell

To overcome these limitations, you can add a combo box to your worksheet, and use programming to make it appear in cells that contain a data validation list.

Instead of clicking the cell's drop down arrow, double-click on a cell that contains a data validation list, and the combo box appears. The combo box's font size can be set, more than 8 rows can be displayed, and autocomplete can be enabled.

Note: If the worksheet is protected, allow users to Edit Objects, and they will be able to use the combobox.

protect sheet options

Video: Data Validation Drop Downs With Combo Box

To see how the combo box works, and appears when you double-click a data validation cell, watch this short video. The written steps are below the video, and the full transcript is available below.

Create a Data Validation Dropdown List

On Sheet1, type the lists that will be used in the data validation dropdowns:

Tip: Use the AutoFill feature to create the lists

  1. In cells K2:K8 type a list of weekdays
  2. In cells M2:M13 type a list of months 

lists on worksheet

The next step is to create the data validation dropdown lists. There are detailed instructions here: Data Validation -- Introduction 

  • Cells C2:C12 have data validation lists with the source K2:K8. When a cell in this range is selected, a dropdown list of weekdays is available.
  • Cells D2:D12 have data validation lists with the source M2:M13. When a cell in this range is selected, a dropdown list of months is available. go to top

dropdown list of months

Add the Combo box

To add or edit the Combobox, open the Control Toolbox, and enter Design Mode:

design mode

  1. Choose View | Toolbars
  2. Select Control Toolbox
  3. Click the Design Mode button
  4. Click on the Combo box button, to activate that tool.
  5. Click on an empty area of the worksheet, to add a combo box go to top

combo box

Open the Properties Window

To format the combo box, open the properties window:

  1. Select the combo box
  2. On the Control Toolbox, click the Properties button

control toolbox properties

Change the Combo box Properties

Name the Combo box

  1. In the Properties window, click in the Name box
  2. Type a name for the combo box. In this example, the name is: TempCombo

TempCombo combo box

Change the Font and Font Size

  1. In the Properties window, click in the Font property, and click the ... button 

    font property

  2. In the Font dialog box, select a font, font size, and other settings that you want for your combo box.

Font dialog box

  1. Click OK  go to top

Set the Number of Rows

  1. In the Properties window, click in the ListRows box
  2. Type the number of rows that you want displayed in the dropdown. In this example, the setting is: 12

Turn on AutoComplete

  1. In the Properties window, click in the MatchEntry property
  2. From the dropdown list, select 1-frmMatchEntryComplete

MatchEntry property

Exit Design Mode

  1. Close the Properties window
  2. On the Control Toolbox, click the Exit Design Mode button

Exit Design Mode

Add the Code

Visual Basic for Applications (VBA) code is required to make the combo box appear when you double-click in a cell that contains a data validation list.

Copy the Code

First, copy one of the code samples below.

  1. Code Sample 1 -- will not work for data validation drop downs in merged cells
  2. Code Sample 2 -- will work for both merged and non-merged cells.

NOTE: If your data validation lists are on a different sheet, use the instructions on this page:
Data Validation Combo Box - Named Ranges     

Code Sample 1 -- Does not work for merged cells

'==========================
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, _
  Cancel As Boolean)
Dim str As String
Dim cboTemp As OLEObject
Dim ws As Worksheet
Set ws = ActiveSheet
Set cboTemp = ws.OLEObjects("TempCombo")
  On Error Resume Next
  With cboTemp
  'clear and hide the combo box
    .ListFillRange = ""
    .LinkedCell = ""
    .Visible = False
  End With
On Error GoTo errHandler
  If Target.Validation.Type = 3 Then
    'if the cell contains a data validation list
    Cancel = True
    Application.EnableEvents = False
    'get the data validation formula
    str = Target.Validation.Formula1
    str = Right(str, Len(str) - 1)
    With cboTemp
      'show the combobox with the list
      .Visible = True
      .Left = Target.Left
      .Top = Target.Top
      .Width = Target.Width + 5
      .Height = Target.Height + 5
      .ListFillRange = ws.Range(str).Address
      .LinkedCell = Target.Address
    End With
    cboTemp.Activate
    'open the drop down list automatically
    Me.TempCombo.DropDown
  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

Set cboTemp = ws.OLEObjects("TempCombo")
  On Error Resume Next
If cboTemp.Visible = True Then
  With cboTemp
    .Top = 10
    .Left = 10
    .ListFillRange = ""
    .LinkedCell = ""
    .Visible = False
    .Value = ""
  End With
End If

errHandler:
  Application.EnableEvents = True
  Exit Sub

End Sub 
'====================================
'Optional code to move to next cell if Tab or Enter are pressed
'from code by Ted Lanham
'***NOTE: if KeyDown causes problems, change to KeyUp

Private Sub TempCombo_KeyDown(ByVal _
        KeyCode As MSForms.ReturnInteger, _
        ByVal Shift As Integer)
    Select Case KeyCode
        Case 9 'Tab 
            ActiveCell.Offset(0, 1).Activate
        Case 13 'Enter 
            ActiveCell.Offset(1, 0).Activate
        Case Else
            'do nothing
    End Select
End Sub
'====================================

Code Sample 2 -- Works for merged cells and non-merged cells

'==========================
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, _
  Cancel As Boolean)
Dim str As String
Dim cboTemp As OLEObject
Dim ws As Worksheet
Dim Tgt As Range
Set Tgt = Target.Cells(1, 1)
Set ws = ActiveSheet
On Error GoTo errHandler

If Tgt.Validation.Type = 3 Then
    Cancel = True
End If

Set cboTemp = ws.OLEObjects("TempCombo")
  On Error Resume Next
  With cboTemp
    .ListFillRange = ""
    .LinkedCell = ""
    .Visible = False
  End With
On Error GoTo errHandler

If Tgt.Validation.Type = 3 Then
    Application.EnableEvents = False
    str = Tgt.Validation.Formula1
    str = Right(str, Len(str) - 1)
    With cboTemp
      .Visible = True
      .Left = Tgt.Left
      .Top = Tgt.Top
      .Width = Target.Width + 15
      .Height = Target.Height + 5
      .ListFillRange = ws.Range(str).Address
      .LinkedCell = Tgt.Address
    End With
    cboTemp.Activate
    'open the drop down list automatically
    Me.TempCombo.DropDown
  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

Set cboTemp = ws.OLEObjects("TempCombo")
  On Error Resume Next
If cboTemp.Visible = True Then
  With cboTemp
    .Top = 10
    .Left = 10
    .ListFillRange = ""
    .LinkedCell = ""
    .Visible = False
    .Value = ""
  End With
End If

errHandler:
  Application.EnableEvents = True
  Exit Sub

End Sub 
'====================================
'Optional code to move to next cell if Tab or Enter are pressed
'from code by Ted Lanham
'***NOTE: if KeyDown causes problems, change to KeyUp
'Table with numbers for other keys such as Right Arrow (39)
'https://msdn.microsoft.com/en-us/library/aa243025%28v=vs.60%29.aspx

Private Sub TempCombo_KeyDown(ByVal _
        KeyCode As MSForms.ReturnInteger, _
        ByVal Shift As Integer)
    Select Case KeyCode
        Case 9 'Tab 
            ActiveCell.Offset(0, 1).Activate
        Case 13 'Enter 
            ActiveCell.Offset(1, 0).Activate
        Case Else
            'do nothing
    End Select
End Sub
'====================================

Add Code to the Worksheet

Follow these steps, to add the copied code to the worksheet module:

  1. Right-click on the sheet tab, and choose View Code.

    View Code

  2. Choose Edit | Paste, to paste the code onto the sheet module, where the cursor is flashing.
  3. Choose File | Close and Return to Microsoft Excel. go to top

Test the Code

  1. Double-click on one of the cells that contains a data validation list.
  2. The combo box will appear
  3. Select an item from the combo box dropdown list

    combo box dropdown list

  4. Click on a different cell, to select it
  5. The selected item appears in previous cell, and the combo box disappears. go to top

TempCombo_KeyDown Code For Numbers

The values that you select in a combo box are treated as text, so that can cause problems if your drop down list contains numbers (including dates and times). In the screen shot below, a time has been selected, and even though the cell is formatted for Time, it appears in the cell as a long decimal number. The entry is really text, not a number, so the number formatting does not affect it.

drop down list contains numbers

To send the numbers to the worksheet as real numbers, instead of text, use the following code, instead of the TempCombo_KeyDown code above.

Then, after you select a number (or date or time) in the combo box drop down list, press the Enter key or the Tab key, to move to the next cell.

'====================================
'Optional code to move to next cell if Tab or Enter are pressed
'from code by Ted Lanham
'***NOTE: if KeyDown causes problems, change to KeyUp
'Table with numbers for other keys such as Right Arrow (39)
'https://msdn.microsoft.com/en-us/library/aa243025%28v=vs.60%29.aspx

Private Sub TempCombo_KeyDown(ByVal _
        KeyCode As MSForms.ReturnInteger, _
        ByVal Shift As Integer)
  'change text value to number, if possible 
  On Error Resume Next
  
  Select Case KeyCode
    Case 9 'Tab - change text to number, move right
      ActiveCell.Value = --ActiveCell.Value
      ActiveCell.Offset(0, 1).Activate
    Case 13 'Enter - change text to number, move down
      ActiveCell.Value = --ActiveCell.Value
      ActiveCell.Offset(1, 0).Activate
    Case Else
        'do nothing
  End Select
End Sub
'====================================

Transcript: Autocomplete Entries With Drop Down List

Here is the full transcript for the Autocomplete Entries With Excel Drop Down List video, shown above.

'==========================

Autocomplete Entries With Excel Drop Down List

To make it easy for people to enter data in Excel, you can create drop down lists of items.

In this cell, we have a list of weekdays. I can click on that Arrow, click on a weekday, and it fills in the cell.

The same thing for months. Here's a list. I can't see all of the months, though. It only shows 8 items at a time, but I can scroll down and click on December and that's in the cell.

Those lists are on another worksheet, so I've just typed the days here, and the months here.

Set Up Data Validation

And then use data validation, which you can find on the Data tab, to set that up.

There are a few limitations though. If we look at the list, the font is very small and you can't change it.

It's not too bad here at 100% zoom, but if you start going down to lower zoom levels, so that you can see more of your worksheet, it might be hard, or almost impossible, to read these lists.

And as we saw for the months, you can only see eight items at a time.

Drop Down List Limitations

It's not too bad here because we only have 12 months, but for a longer list you might need to do quite a bit of scrolling up and down to find items.

This doesn't auto complete. Data validation doesn't help you fill in the word.

Excel might help you, if it can copy something that you've already entered above, but the data validation itself won't help me type January.

If I start typing, I have to fill in the whole word and move on to the next cell.

Add a Combo Box

So data validation is helpful, but I've come up with a work around that you can download on my website and it uses a combo box over cells that have a data validation drop down.

In this corner, hidden away, is a little combo box, and it pops up over a cell, when you double click on it.

So if I double click here, the Combo box comes out of hiding, goes over this cell, and picks up the same data validation list that the cell uses.

So when I click the arrow, I'll see the list of weekdays. I can click on one, press Enter to go to the next cell.

I'll double click again, and for this one I could start typing T, and it fills in Tuesday, but if I wanted Thursday, I'll type in h, and then press Enter.

Adjust the Combo Box

So it helps with the typing, and if we look at the months, I've changed the number of rows that show, so we can see 12.

You can adjust that setting, as well as the font size, and select. And again here, we can type.

So if I want to type August, just have to type two letters and it's filled in for me.

To see the Combo box, I'm going to double click on a cell and then go to the Developer tab on the ribbon, and there's a Design Mode button.

When I click that, I'm able to make changes to that Combo box. I can click on the Properties button and here are all the things you could change about that Combo box.

So you get a lot more control over it than you have over data validation.

Change Combo Box Appearance

So you could come down here, and go to the font. Instead of Arial font, you could change the font, and the font size, to whatever suits you.

And here's where I changed the list rows. This is 12 you could make that a larger number. You'd want to pick a number that's going to fit on the screen. You don't want anything that's too huge, but you can adjust that setting.

And then when you're done making changes, turn off Design Mode, and you're ready to go again.

Get the Sample File and Code

So if you download the sample file from my website, you'll be able to copy it all into your workbook.

Copy the Combo box and the code onto the worksheet. So if I right click View Code, you'll see that there's code that runs, when you double click the cell.

You don't have to understand all this code, you would just have to get it into your workbook.

For more Excel tips and tutorials, and to download the sample file for this video, please visit my Contextures website, at www.contextures.com

Get the Sample File

Get the zipped sample for this tutorial -- Data Validation Combo box file

Related Tutorials

Data Validation Basics

Combo box Resources

Data Validation Combo - Lists on Other Sheet

Data Validation Combo - Click


Last updated: April 4, 2022 7:14 PM