Search Contextures Sites

 

Learn how to create Excel dashboards.

 

 

 

Learn how to create Excel dashboards.

Excel VBA ComboBox Match Required

Create a ComboBox  
Single Column ComboBox  
Multiple Column ComboBox  
Prevent Invalid Entries in the ComboBox  
Add "Select" Item to ComboBox lists 
Download a Sample Excel VBA ComboBox Match file  

Watch the Excel VBA ComboBox video

 

Create a ComboBox

This Excel VBA example is based on the instructions for Creating an Excel UserForm With ComboBoxes. On that page, the lists are added to the comboboxes by looping through a named range.

In this example, there are a couple of improvements to the UserForm:

  • a simpler technique is used to create the lists.
  • the comboboxes are modified so that only items that match a list item can be entered.

We will be working with the two comboboxes shown below -- a single column combo box for Location,

Excel ComboBox One Column

and a two-column ComboBox for Parts, showing the Part ID, and the part name..

Excel ComboBox Multiple Column

Single Column ComboBox

The Location combobox is named cboLocation. It should show all the locations from a named range -- LocationList -- on the LookupLists worksheet.

Location List

To set the combobox to use LocationList as its source:

  1. In the Visual Basic Editor (VBE), select the UserForm, and click on the Location combobox, to select it.
  2. In the Properties window, find the RowSource property, and enter LocationList as the source.

    RowSource property

Multiple Column ComboBox

Next, we'll add items to the Part ComboBox, which using the PartsLookup named range. The Part combobox will have 2 columns -- Part ID and Part description

PartsLookup range

To set the Part combobox to use PartsLookup as its source:

  1. In the Visual Basic Editor (VBE), select the UserForm, and click on the Part combobox, to select it.
  2. In the Properties window, find the RowSource property, and enter PartsLookup as the source.

    RowSource property

Prevent Invalid Entries in the ComboBox

To prevent invalid entries in the comboboxes, change the ComboBox properties, so a match is required.

  1. Select the Part combobox, and in the Properties window, change the MatchRequired setting to True.
  2. Repeat this setting change for the Location combobox.

MatchRequired property

Add "Select" Item to ComboBox lists

Setting the MatchRequired property to True works well, unless your Excel VBA code tries to clear out the comboboxes, to prepare for a new entry. In this example, when you click the Add This Part button, the code removes the entries from the Part combobox and the Location combobox.

MatchRequired property

When that happens, an error message appears, warning "Invalid Property Value."

Invalid Property Value

To prevent that error, add a "Select" item at the top of each named range.

Select item in list

Then, in the code, add "Select" to the combobox, after the Add Part button is clicked, instead of leaving the comboboxes blank.

Select item in list

Now, the first item in the combobox drop down list is Select, and only items from the list can be entered.

Select item in list

Watch the Excel VBA ComboBox Match video

 

Download a Sample Excel VBA ComboBox Match file

Download the zipped sample Excel UserForm ComboBox Match file to see the completed UserForm and Excel VBA code.

Watch the Excel VBA ComboBox Match video

 

 

       Home     Excel Tips     Excel Files     The Excel Store     Blog     Contact

 

Privacy Policy

 

Contextures Inc., Copyright ©2012
All rights reserved.

 

Last updated: June 11, 2011 8:02 PM