This article was previously published under Q213363
This article has been archived. It is offered "as is" and will no longer be updated.
This article describes a sample Microsoft Visual Basic for ApplicationsSub procedure (macro) that enters a selected item from a list box into the active cell on a worksheet.
Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Partner or Microsoft Advisory Services. For more information, visit these Microsoft Web sites:
For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMSTo create and use a sample macro to enter a selected item from a list box into the active cell on a worksheet, use the steps in the following example:
Start Excel and open a new worksheet.
Enter the following data in the worksheet:
A1: One A2: Two A3: Three
On the View menu, point to Toolbars, and then click to select the Forms checkbox (if not already checked).
On the Forms toolbar, click List Box.
Draw a list box on the worksheet.
Right-click the list box to select it (if it is not already selected).
On the Format menu, click Control.
Click the Control tab.
In the Input range box, type A1:A3, and then click OK.
Press ALT+F11 to start the Visual Basic editor.
On the Insert menu, click Module.
In the module sheet, type the following code:
Sub Test() Dim Lbox As Object ' Create the Lbox object, which will be the list box on the Active ' sheet. ' Activesheet can be changed to sheets("test") where test is the ' name of the sheet on which you want the information. Set Lbox = ActiveSheet.ListBoxes(1) ' Check to see the value (or what is selected) of the list box. If ' zero, exit the Sub. If Lbox.Value = 0 Then Exit Sub ' Change the value of the Active cell to be what is selected in the ' list box ActiveCell.Value = Lbox.List(Lbox.Value) ' Set the list box back to zero or nothing selected. Lbox.Value = 0End Sub
Right-click the list box control.
On the shortcut menu, click Assign Macro.
In the Macro name list, click Test, and then click OK.
Select Cell A4 (or any blank cell).
On the Tools menu, point to Macro, and then click Macros.
In the Macro name list, click Test, and then click Run.
In the list box, click Two.Cell A4 returns Two.