Anda di halaman 1dari 10

How to Create a Listbox in VB From an

Excel Spreadsheet
A listbox can allow users to select data from a list for entry into a particular cell.

For example, to enter data about bank accounts into a spreadsheet, a user might select
an option from "personal" or "business" accounts.

Listboxes are generally created through a series of menus on the ribbon (the top
toolbar).

Microsoft Office Visual Basic for Applications allows you to create a listbox using a
short piece of code instead of clicking through menus.

Step 1-Open up the Visual Basic Editor (VBE) by pressing "Alt" and "F11" from the
Excel worksheet.

Step 2-Open a new module (a blank page) by clicking on "Insert" and then choosing
"Module."
Step3-Type the following into the module:
-----------------------------------------------------------------------------------
Sub listbox()
' listbox Macro
ActiveSheet.ListBoxes.Add(229.2, 52.2, 72, 69.6).Select
With Selection
.ListFillRange = "$A$2:$A$4"
.LinkedCell = "$A$2:$A$4"
.MultiSelect = xlNone
.Display3DShading = False
End With
Range("E3").Select
End Sub

Add a list box (Form control)

1. If the Developer tab is not available, display it.

Display the Developer tab

1. Click the Microsoft Office Button , and then click Excel Options.
2. In the Popular category, under Top options for working with Excel,
select the Show Developer tab in the Ribbon check box, and then
click OK.

 Note   The Ribbon is a component of the Microsoft Office Fluent user


interface.
2. On the Developer tab, in the Controls group, click Insert, and then under
Form Controls, click List box .

3. Click the worksheet location where you want the upper-left corner of the list
box to appear.
4. On the Developer tab, in the Controls group, click Properties .

 Tip   You can also right-click the control, and then click Format Control.

To specify the control properties, do the following:

1. In the Input range box, enter a cell reference to a range that contains
the values to display in the list box.
2. In the Cell link box, enter a cell reference that contains the list box
selection.

The linked cell returns the number of the selected item in the list box.
The first item in the range returns a value of 1, the second item in the
range returns a value of 2, and so on.

Use this number in a formula to return the actual item from the input
range.

For example, a dessert preference form has a list box that is linked to
cell C1, the input range for the list is D1:D5, and the items in the range
are: "Ice Cream" (D1), "Cake" (D2), "Liqueur" (D3), "Candy" (D4),
and "Chocolate" (D5). The following formula, entered in cell B1,
returns the value "Liqueur" from range D1:D5 if the value of C1 is 3,
based on the current selection in the list box.

=INDEX(D1:D5,C1)

3. Under Selection type, specify how items can be selected in the list box
by doing one of the following:
 To create a single-selection list box, click Single.
 To create a multiple-selection list box, click Multi.
 To create an extended-selection list box, click Extend.

 Note   If you set the selection type to Multi or Extend, the cell that is
specified in the Cell link box returns a value of 0 and is ignored. The
Multi and Extend selection types require the use of Microsoft Visual
Basic for Applications (VBA) code. In these cases, consider using the
ActiveX list box control.
 Top of Page

Add a list box (ActiveX control)

1. If the Developer tab is not available, display it.

Display the Developer tab

1. Click the Microsoft Office Button , and then click Excel Options.
2. In the Popular category, under Top options for working with Excel,
select the Show Developer tab in the Ribbon check box, and then
click OK.

 Note   The Ribbon is a component of the Microsoft Office Fluent user


interface.

2. On the Developer tab, in the Controls group, click Insert, and then under
ActiveX Controls, click List Box .

3. Click the worksheet location where you want the upper-left corner of the list
box to appear.
4. To edit the ActiveX control, make sure that you are in design mode. On the
Developer tab, in the Controls group, turn on Design Mode .
5. To specify the control properties, on the Developer tab, in the Controls
group, click Properties .

 Tip   You can also right-click the control, and then click Properties.

The Properties dialog box appears. For detailed information about each
property, select the property, and then press F1 to display a Visual Basic
Help (Visual Basic Help: To get Help for Visual Basic, point to Macro on the
Tools menu, and then click Visual Basic Editor. On the Help menu, click
Microsoft Visual Basic Help.) topic. You can also type the property name in
the Visual Basic Help Search box. The following section summarizes the
properties that are available.

Summary of properties by functional categories


If you want to specify Use this property
General:  
Whether the control is loaded when the
workbook is opened. (Ignored for ActiveX AutoLoad (Excel)
controls.)
Whether the control can receive the focus and
Enabled (Form)
respond to user-generated events.
Whether the control can be edited. Locked (Form)
The name of the control. Name (Form)
The way the control is attached to the cells
below it (free floating, move but do not size, or Placement (Excel)
move and size).
Whether the control can be printed. PrintObject (Excel)
Whether the control is visible or hidden. Visible (Form)
Text:  
Bold, Italic, Size,
Font attributes (bold, italic, size, strikethrough,
StrikeThrough, Underline,
underline, and weight).
Weight (Form)
The default run time mode of the Input Method
IMEMode (Form)
Editor (IME).
Whether the size of the control adjusts to display
IntegralHeight (Form)
full or partial lines of text.
Whether multiple selections of items are
MultiSelect (Form)
permitted.
The text in the control. Text (Form)
How text is aligned in the control (left, center, or
TextAlign (Form)
right).
Data and Binding:  
The range that is linked to the control's value. LinkedCell (Excel)
The content or state of the control. Value (Form)
Size and Position:  
The height or width in points. Height, Width (Form)
The distance between the control and the left or
Left, Top (Form)
top edge of the worksheet.
Formatting:  
The background color. BackColor (Form)
The color of the border. BorderColor (Form)
The type of border (none or single-line). BorderStyle (Form)
The foreground color. ForeColor (Form)
Whether the control has a shadow. Shadow (Excel)
The visual appearance of the border (flat, raised,
SpecialEffect (Form)
sunken, etched, or bump).
Keyboard and Mouse:  
A custom mouse icon. MouseIcon (Form)
The type of pointer that is displayed when the
user positions the mouse over a particular object MousePointer (Form)
(for example, standard, arrow, or I-beam).
Specific to List Box:  
The source of data for multiple columns. BoundColumn (Form)
The number of columns to display. ColumnCount (Form)
A single row as a column heading. ColumnHeads (Form)
The width of each column. ColumnWidths (Form)
The range that is used to populate the list. ListFillRange (Excel)
The list style (plain, with option buttons, or with
ListStyle (Form)
check boxes).
How the control searches its list while the user
MatchEntry (Form)
types (first letter, complete entry, or none)
The column to store in the Text property when
TextColumn (Form)
the user selects a row.
The item that appears in the topmost position in
TopIndex (Form)
the list.

  Notes  

 To create a list box with multiple selection or extended-selection enabled, use


the MultiSelect property. In this case, the LinkedCell property returns a #N/A
value. You must use VBA code to process the multiple selections.
 To create a two-column list box with column headers, set ColumnCount to 2,
ColumnHeads to True, ColumnWidths to the width that you want for each
column (for example, 72pt;72pt), ListFillRange to the range that is used to
populate the list (for example, B2:C6), BoundColumn to either 1 or 2 to
indicate which column value to save, and LinkedCell to a cell address that
contains the selected value. By default, the column label is used as the column
header (for example, Column B and Column C). To use your own column
headers, place them immediately above the first value specified in
ListFillRange (for example, B1 and C1) before you close the Properties
dialog box. Finally, resize the list box to display both columns.
 To create a list box that displays one value in the list box but saves another
value in the linked cell, create a two-column list box, and then hide one of the
columns by setting its ColumnWidths value to 0. For example, you can set up
a two-column list box that contains the names of holidays in one column and
dates associated with the holidays in a second column. To present the holiday
names to users, specify the first column as the TextColumn. To store the
dates of the holidays, specify the second column as the BoundColumn. To
hide the dates of the holidays, set the ColumnWidths property of the second
column to 0.

 Top of Page

Add a combo box (Form control)

1. If the Developer tab is not available, display it.

Display the Developer tab

1. Click the Microsoft Office Button , and then click Excel Options.
2. In the Popular category, under Top options for working with Excel,
select the Show Developer tab in the Ribbon check box, and then
click OK.
 Note   The Ribbon is a component of the Microsoft Office Fluent user
interface.

2. On the Developer tab, in the Controls group, click Insert, and then under
Form Controls, click Combo box .

3. Click the worksheet location where you want the upper-left corner of the
combo box to appear.

The drop-down arrow is displayed with the text box collapsed.

4. To display the text box, drag the left-center sizing handle to the right.
5. On the Developer tab, in the Controls group, click Properties .

 Tip   You can also right-click the control, and then click Format Control.

To specify the control properties, do the following:

1. In the Input range box, enter a cell reference to a range that contains
the values to display in the drop-down list of the combo box.
2. In the Cell link box, enter a cell reference that contains the selection in
the drop-down list of the combo box.

The linked cell returns the number of the selected item in the drop-
down list of the combo box. The first item in the range returns a value
of 1, the second item in the range returns a value of 2, and so on.

Use this number in a formula to return the actual item from the input
range. For example, a dessert preference form has a combo box linked
to cell C1, the input range for the list is D1:D5, and the items in the
range are: "Ice Cream" (D1), "Cake" (D2), "Liqueur" (D3), "Candy"
(D4), and "Chocolate" (D5). The following formula, entered in cell B1,
returns the value "Liqueur" from range D1:D5 if the value of C1 is 3,
based on the current selection in the combo box.

=INDEX(D1:D5,C1)

 Note   If you want to create a combo box that enables the user to edit
the text in the text box, consider using the ActiveX Combo Box
control.

3. In the Drop-down lines box, enter the number of lines to display in the
drop-down list of the combo box. If the value is:
 0, it is ignored and treated as 1.
 Less than the number of items in the range specified in the
Input range box, a scroll bar is displayed.
 Equal to or greater than the number of items in the range
specified in the Input range box, no scroll bar is displayed.

 Top of Page

Add a combo box (ActiveX control)

1. If the Developer tab is not available, display it.

Display the Developer tab

1. Click the Microsoft Office Button , and then click Excel Options.
2. In the Popular category, under Top options for working with Excel,
select the Show Developer tab in the Ribbon check box, and then
click OK.

 Note   The Ribbon is a component of the Microsoft Office Fluent user


interface.

2. On the Developer tab, in the Controls group, click Insert, and then under
ActiveX Controls, click Combo Box .

3. Click the worksheet location where you want the upper-left corner of the
combo box to appear.
4. To edit the ActiveX control, make sure that you are in design mode. On the
Developer tab, in the Controls group, turn on Design Mode .
5. To specify the control properties, on the Developer tab, in the Controls
group, click Properties .

 Tip   You can also right-click the control, and then click Properties.

The Properties dialog box appears. For detailed information about each
property, select the property, and then press F1 to display a Visual Basic
Help (Visual Basic Help: To get Help for Visual Basic, point to Macro on the
Tools menu, and then click Visual Basic Editor. On the Help menu, click
Microsoft Visual Basic Help.) topic. You can also type the property name in
the Visual Basic Help Search box. The following section summarizes the
properties that are available.
Summary of properties by functional categories
If you want to specify Use this property
General:  
Whether the control is loaded when the
workbook is opened. (Ignored for ActiveX AutoLoad (Excel)
controls.)
Whether the control can receive the focus
Enabled (Form)
and respond to user-generated events.
Whether the control can be edited. Locked (Form)
The name of the control. Name (Form)
The way the control is attached to the cells
below it (free floating, move but do not Placement (Excel)
size, or move and size).
Whether the control can be printed. PrintObject (Excel)
Whether the control is visible or hidden. Visible (Form)
Text:  
Whether a word or a character is the basic
AutoWordSelect (Form)
unit used to extend a selection.
Bold, Italic, Size,
Font attributes (bold, italic, size,
StrikeThrough, Underline,
strikethrough, underline, and weight).
Weight (Form)
Whether selected text remains highlighted
HideSelection (Form)
when the control does not have the focus.
The default run time mode of the Input
IMEMode (Form)
Method Editor (IME).
The maximum number of characters a user
MaxLength (Form)
can enter.
Whether the user can select a line of text by
SelectionMargin (Form)
clicking to the left of the text.
The text in the control. Text (Form)
How text is aligned in the control (left,
TextAlign (Form)
center, or right).
Data and binding:  
The range that is linked to the control's
LinkedCell (Excel)
value.
The content or state of the control. Value (Form)
Size and position:  
Whether the size of the control
automatically adjusts to display all the AutoSize (Form)
contents.
The height or width in points. Height, Width (Form)
The distance between the control and the
Left, Top (Form)
left or top edge of the worksheet.
Formatting:  
The background color. BackColor (Form)
The background style (transparent or
BackStyle (Form)
opaque).
The color of the border. BorderColor (Form)
The type of border (none or single-line). BorderStyle (Form)
The foreground color. ForeColor (Form)
Whether the control has a shadow. Shadow (Excel)
The visual appearance of the border (flat,
SpecialEffect (Form)
raised, sunken, etched, or bump).
Keyboard and mouse:  
Whether an automatic tab action occurs
after a user has entered the maximum AutoTab (Form)
number of characters for the control.
Whether drag-and-drop is enabled. DragBehavior (Form)
The selection behavior when entering the
EnterFieldBehavior (Form)
control (select all or do not change).
A custom mouse icon. MouseIcon (Form)
The type of pointer that is displayed when
the user positions the mouse over a
MousePointer (Form)
particular object (for example, standard,
arrow, or I-beam).
Specific to combo box:  
The source of data for multiple columns. BoundColumn (Form)
The number of columns to be displayed. ColumnCount (Form)
A single row as a column heading. ColumnHeads (Form)
The width of each column. ColumnWidths (Form)
The symbol that is displayed on the drop
button (down arrow, plain, ellipsis, or DropButtonStyle (Form)
underscore).
The range that is used to populate the list. ListFillRange (Excel)
The maximum number of rows to display in
ListRows (Form)
the list.
The list style (plain, with option buttons, or
ListStyle (Form)
with check boxes).
The width of the list. ListWidth (Form)
How the control searches its list while the
user types (first letter, complete entry, or MatchEntry (Form)
none)
Whether a value entered as text must match
MatchRequired (Form)
an entry in the existing list.
When to show the drop button (never, with
ShowDropButtonWhen (Form)
focus, or always).
How the user chooses or sets the value
Style (Form)
(drop-down combo or drop-down list).
The column to store in the Text property
TextColumn (Form)
when the user selects a row.
The item that appears in the topmost
TopIndex (Form)
position in the list.

  Notes  
 To create a two-column combo box with column headers, set ColumnCount
to 2, ColumnHeads to True, ColumnWidths to the width that you want for
each column (for example, 72pt;72pt), ListFillRange to the range that is used
to populate the list (for example, B1:C6), BoundColumn to either 1 or 2 to
indicate which column value to save, TextColumn to the column of data that
you want displayed in the text box section of the combo box (which can be the
same as or different from the BoundColumn value), and LinkedCell to a cell
address that will contain the selected value. By default, the column label is
used as the column header (for example, Column B and Column C). To use
your own column headers, place them immediately above the first value
specified in ListFillRange (for example, B1 and C1) before you close the
Properties dialog box. Finally, resize the combo box to display both columns.
 To create a combo box that displays one value in the combo box but saves
another in the linked cell, create a two-column combo box, and then hide one
of the columns by setting its ColumnWidths value to 0. For example, you can
set up a two-column combo box that contains the names of holidays in one
column and associated dates for the holidays in a second column. To present
the holiday names to users, specify the first column as the TextColumn. To
store the dates of the holidays, specify the second column as the
BoundColumn. To hide the dates of the holidays, set the ColumnWidths
property of the second column to 0.
 To create a combo box that does not enable the user to enter new values, set
Style to 2. To create a combo box that enables a user to enter new values that
are not found in the list, set Style to 1, which is the default. In this case, you
must write VBA code if you want to dynamically update the list values.

Anda mungkin juga menyukai