Microsoft KB Archive/213363

= XL2000: How to Programmatically Insert Data from a List Box into the Active Cell =

Article ID: 213363

Article Last Modified on 11/23/2006

-

APPLIES TO


 * Microsoft Excel 2000 Standard Edition

-



This article was previously published under Q213363





SUMMARY
This article describes a sample Microsoft Visual Basic for Applications Sub procedure (macro) that enters a selected item from a list box into the active cell on a worksheet.



MORE INFORMATION
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 the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site:

https://partner.microsoft.com/global/30000104

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;CNTACTMS

To 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).</li> On the Format menu, click Control.</li> Click the Control tab.</li> In the Input range box, type A1:A3, and then click OK.</li> Press ALT+F11 to start the Visual Basic editor.</li> On the Insert menu, click Module.</li>  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 = 0 End Sub </li> Right-click the list box control.</li> On the shortcut menu, click Assign Macro.</li> In the Macro name list, click Test, and then click OK.</li> Select Cell A4 (or any blank cell).</li> On the Tools menu, point to Macro, and then click Macros.</li> In the Macro name list, click Test, and then click Run.</li> In the list box, click Two. Cell A4 returns Two.</li></ol>

Additional query words: listbox XL2000

Keywords: kbdtacode kbhowto kbprogramming KB213363

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.