XL: How to Add Data to a Drop-Down or List Box

ID: Q141573


The information in this article applies to:
  • Microsoft Excel for Windows, versions 5.0, 5.0c
  • Microsoft Excel for the Macintosh, versions 5.0, 5.0a
  • Microsoft Excel for Windows NT, version 5.0
  • Microsoft Excel for Windows 95, versions 7.0, 7.0a
  • Microsoft Excel 97 for Windows
  • Microsoft Excel 98 Macintosh Edition


SUMMARY

In Microsoft Excel, there are two ways of populating a list or drop-down box: you can link worksheet data to the control, or you can run a Microsoft Visual Basic for Applications macro to add data to the control.


MORE INFORMATION

To link the list box or drop-down box to a range of cells on a worksheet

  1. Create a list box or drop-down on a custom dialog box.


  2. Select the control.


  3. On the Format menu, click Object, and click the Control tab.


  4. In the Input Range box, enter a reference to a vertical range of cells (for example, Sheet1!$A$1:$A$10).

    Do not include a header row unless you want that row to be included in the list or drop-down box.


To use a Visual Basic Macro to populate the drop-down or list box

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 the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:
http://www.microsoft.com/support/supportnet/overview/overview.asp
To create the dialog box:

  1. Insert a dialog sheet. Name this sheet Dialog1.

    NOTE: To name a dialog box element, type the name in the Name box (the box at the left end of the formula bar).


  2. Create a list box control on the dialog box. Name the list box List Box 4.


  3. Create a drop-down box on the dialog box. Name drop-down box Drop Down 5.


  4. In a module sheet, enter the following macro code:
    
          Sub Fill_Control()
             Dim diag As Object
             Dim mylist As Object
             Dim mydrop As Object
    
             Set diag = DialogSheets("Dialog1")
             Set mylist = diag.ListBoxes("List Box 4")
             Set mydrop = diag.DropDowns("Drop Down 5")
    
             'Remove all items from drop-down and list box
             mylist.RemoveAllItems
             mydrop.RemoveAllItems
    
             'Insert data into List Box and Drop Down
             myarray = Array("Tom", "Fred", "Sam", "Wilma", "Sandy")
    
             For x = 0 To 4
                mylist.AddItem myarray(x)
                mydrop.AddItem myarray(x)
             Next x
    
             'Show Dialog Box
             diag.Show
    
          End Sub 


  5. To run the macro, position the insertion point on the line that reads,
    
       Sub Fill_Control() 
    and press F5.


Switch to the dialog sheet and click the Run Dialog button to run the dialog box. When you use each of the controls, note that they contain the data from the arrays that you created in the macro. To quit the dialog box, click Enter or Cancel.


REFERENCES

"Visual Basic User's Guide," version 5.0, page 231

Additional query words: 97 98 XL98 XL97 XL7 XL5

Keywords : kbcode kbprg PgmHowto
Version : MACINTOSH:5.0,5.0a,98; WINDOWS:5.0,5.0c,7.0,7.0a,97; winnt:5.0
Platform : MACINTOSH WINDOWS winnt
Issue type : kbhowto


Last Reviewed: November 9, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.