Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

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


View products that this article applies to.

This article was previously published under Q213363

↑ Back to the top


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.

↑ Back to the top


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 Microsoft Advisory Services. For more information, visit these Microsoft Web sites:

Microsoft Certified Partners - https://partner.microsoft.com/global/30000104

Microsoft Advisory Services - http://support.microsoft.com/gp/advisoryservice

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:
  1. Start Excel and open a new worksheet.
  2. Enter the following data in the worksheet:
    A1: One
    A2: Two
    A3: Three
  3. On the View menu, point to Toolbars, and then click to select the Forms checkbox (if not already checked).
  4. On the Forms toolbar, click List Box.
  5. Draw a list box on the worksheet.
  6. Right-click the list box to select it (if it is not already selected).
  7. On the Format menu, click Control.
  8. Click the Control tab.
  9. In the Input range box, type A1:A3, and then click OK.
  10. Press ALT+F11 to start the Visual Basic editor.
  11. On the Insert menu, click Module.
  12. 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
    					
  13. Right-click the list box control.
  14. On the shortcut menu, click Assign Macro.
  15. In the Macro name list, click Test, and then click OK.
  16. Select Cell A4 (or any blank cell).
  17. On the Tools menu, point to Macro, and then click Macros.
  18. In the Macro name list, click Test, and then click Run.
  19. In the list box, click Two. Cell A4 returns Two.

↑ Back to the top


Keywords: KB213363, kbprogramming, kbhowto, kbdtacode

↑ Back to the top

Article Info
Article ID : 213363
Revision : 8
Created on : 11/23/2006
Published on : 11/23/2006
Exists online : False
Views : 296