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



The information in this article applies to:

  • Microsoft Excel 2000

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: For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site: 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.

Modification Type:MinorLast Reviewed:10/11/2006
Keywords:kbdtacode kbhowto kbProgramming KB213363