Академический Документы
Профессиональный Документы
Культура Документы
html)
NOTE: You can't use ActiveX controls on a Macintosh. Prof. Lee Townsend shows how to create drop downs using Form control combo boxes
(http://uhaweb.hartford.edu/ltownsend/excel.html).
Introduction
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.
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.
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.
NOTE: If you are using lists in formatted Excel tables, create a named range based on the table column. Then, create a second named range, based on that first name. Otherwise, the
combo box list will be empty. See detailed instructions on this blog post (http://blog.contextures.com/archives/2014/09/18/worksheet-combo-box-problem-in-excel-2013/).
Cells B2:B12 have data validation lists with the source DayList. When a cell in this range is selected, a dropdown list of weekdays is available.
Cells C2:C12 have data validation lists with the source MonthList. When a cell in this range is selected, a dropdown list of months is available.
1. On the Ribbon, click the Developer tab. If you do not see the Developer tab, follow the steps here (http://www.contextures.com/excel-macro-record-test.html#ReadyTest) to
show it.)
2. Click the Design Mode command
3. Click Insert, and under ActiveX Controls, click on the Combo box button, to activate that tool.
2. In the Font dialog box, select a font, font size, and other settings that you want for your combo box, then click OK.
NOTE: For dates or numbers in the data validation, you can use the KeyDown code in the Code for Numbers section below.
'==========================
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
errHandler:
Application.EnableEvents = True
Exit Sub
End Sub
'=========================================
Private Sub TempCombo_LostFocus()
With Me.TempCombo
.Top = 10
.Left = 10
.Width = 0
.ListFillRange = ""
.LinkedCell = ""
.Visible = False
.Value = ""
End With
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
Worksheet_BeforeDoubleClick
This code runs when a cell is double-clicked.
Variables are set for the active sheet, and the combo box named TempCombo -- be sure to use that name for your combo box.
Set ws = ActiveSheet
The combo box is hidden, and its linked cell and ListFillRange are cleared.
With cboTemp
'clear and hide the combo box
.ListFillRange = ""
.LinkedCell = ""
.Visible = False
End With
The data validation type for the Target cell (the active cell) is checked. If it is Type 3 (a drop down list), the rest of the code runs.
If Target.Validation.Type = 3 Then
The str variable gets the data validation formula for the Target cell. For example: "=MonthList". Then, the equal sign is removed, by using the Right function. That leaves just the
Range name for the data validation list -- "MonthList"
str = Target.Validation.Formula1
str = Right(str, Len(str) - 1)
The combo box is made visible, an is positioned at the top left of the Target cell
With cboTemp
'show the combobox with the list
.Visible = True
.Left = Target.Left
.Top = Target.Top
The combo box width and height are determined by the Target cell's width and height, with a small amount added. You can change the "+5" to a different number:
.Width = Target.Width + 5
.Height = Target.Height + 5
The ListFillRange is changed to the str variable -- MonthList -- so the combo box will show the items from that named range.
.ListFillRange = str
.LinkedCell = Target.Address
The combo box is activated, and the drop down list is opened.
cboTemp.Activate
Me.TempCombo.DropDown
End If
TempCombo_LostFocus
This code runs when you exit the combo box
The combo box is hidden, moved to the top left of the worksheet, and its linked cell and ListFillRange are cleared.
With Me.TempCombo
.Top = 10
.Left = 10
.Width = 0
.ListFillRange = ""
.LinkedCell = ""
.Visible = False
.Value = ""
End With
Follow these steps to locate the combo box, and adjust its properties:
4. In the Properties window, select TempCombo from the drop down list
5. Adjust the properties that you want to change
NOTE: The combo box width and height are set in the macro -- changing these values in the property window will not have a long term effect. Edit the code, if you want the size
to change.
6. When finished, close the Property window, and click the Design button, to exit Design mode.
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.
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.
NOTE: This code is used in the sample file, that you can download below. You can test it in column D on the Validation Sample sheet.
'====================================
'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
In the existing code on the ValidationSample sheet module, look for the following lines:
Application.EnableEvents = False
str = Target.Validation.Formula1
str = Right(str, Len(str) - 1)
Below those lines, add the following lines of code, to handle simple INDIRECT formulas, such as =INDIRECT(E2)
This is used for the City drop down list in the Dependent Combo sample file below.
'for simple INDIRECT function (English)
' e.g. =INDIRECT(B2)
'will create dependent list of items
If Left(str, 4) = "INDI" Then
lSplit = InStr(1, str, "(")
str = Right(str, Len(str) - lSplit)
str = Left(str, Len(str) - 1)
str = Range(str).Value
End If
In the premium version, a list box automatically appears when you select a cell that has a drop down list. You can set it to allow selection of a single item (button), or multiple items.
(http://www.contextures.com/datavalidationmultiselectpremium.html)
(http://www.contextures.com/datavalidationmultiselectpremium.html)
For multiple selections, the listbox pre-selects any items that are already in the cell. It also has buttons to Clear all the selections, and select all the items.
The kit has a setup sheet, that lets you quickly customize the listbox, and details on how to add this technique to your own workbooks. See the details here: Data Validation Multi Select
Premium (http://www.contextures.com/datavalidationmultiselectpremium.html).
Download the Sample File
1. To test the combo box code, you can download the zipped sample file (DataValComboboxSheet.zip).
Related Links
Data Validation Multi Select Premium (http://www.contextures.com/datavalidationmultiselectpremium.html)
Get Excel
News
Name:
Email:
Get Started
(30excelfunctionsin30days01.html)
(http://www.contextures.com/datavalidationmultiselectpremium.html)