+ Reply to Thread
Results 1 to 19 of 19

Populate list box from a range of cells?

  1. #1
    Registered User
    Join Date
    08-18-2007
    Posts
    54

    Populate list box from a range of cells?

    How do you populate a form list box from a range of cells e.g. A1:A10?

  2. #2
    Registered User
    Join Date
    01-13-2007
    Posts
    71
    i find it easier to do it naming the range you want to populate.
    and then you can use the following code

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    08-18-2007
    Posts
    54
    Excellent, that has solved part one,

    When a user selects Item 1 in the ComboBox I want the label next to it to read the Item Code.

    The item code would be in the adjacent cell to the description.

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Are you using the ListBox on a userform/

    Why are you using a listBox and a label when a listbox can have multicolumns and so display the contents of each column?
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  5. #5
    Registered User
    Join Date
    01-13-2007
    Posts
    71
    well... im going to show you how you could write this code, but im pretty sure you could do the same thing going a different way.

    for instance, listbox1 is populated from range A1:A10, and the cells you want to display are the correspondent cell on collumn B.

    Please Login or Register  to view this content.
    i guess this solves your problem. i didnt try this code to see if it works properly, so if you have problems let us know.

  6. #6
    Registered User
    Join Date
    08-18-2007
    Posts
    54
    Hi Roy,

    I have used a combo box. Would a list box be more suitable? I am trying to achieve a form whereby;

    If a user selects Item1 the code for the item is auto populated onto the form. All the items are listed in sheet1 with Item Description in Column A and Item Code in Column B.

    What would be the most efficient way to achieve this?

  7. #7
    Registered User
    Join Date
    08-18-2007
    Posts
    54
    marcospaterson;

    I get a compile error when changing the value of the combobox;

    Method or data member not found

  8. #8
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    I still don't know whether you are using a UserForm or the Forms Controls from the Forms menu. Also, you originally stated that you are using a listBox.

  9. #9
    Registered User
    Join Date
    08-18-2007
    Posts
    54
    Hi Roy i am using a UserForm and I was using a combox. Sorry for the confusion

  10. #10
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Well I would use a listBox as originally stated. Set the Column Count to two. Then load the listbox

    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    08-18-2007
    Posts
    54
    Hi Roy,

    I seem to have got a little confused and can't get this to work. I need a combo box really as I don't like the way list boxes show the data.

  12. #12
    Registered User
    Join Date
    08-18-2007
    Posts
    54
    I get an errorwith Label9.Value =, Method or Data member not found

    Please Login or Register  to view this content.

  13. #13
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Don't use the select method it is inefficient, use the .Find function.
    Your error is caused because a label does not have a Value Property, but a Caption.

    Please Login or Register  to view this content.

  14. #14
    Registered User
    Join Date
    08-18-2007
    Posts
    54
    Thanks Roy,

    Only other problem now is that it will only work when my active sheet1 is selected. So if I am viewing Sheet2 when the form runs i get an error.

    Method 'Range' of object '_worksheet' failed.

    I have fixed it using 'Sheet1.Select' before the rng selection. Would you say thats an appropriate fix?

    Many Thanks for you help
    Sam Williams

  15. #15
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    I wouldn't select sheets unnecessarily. You need to specify the sheet in your code and when loading the combobox. Can you attach a zipped workbook example?

  16. #16
    Registered User
    Join Date
    08-18-2007
    Posts
    54
    I have attached the xls file
    Attached Files Attached Files

  17. #17
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Here's some amended code for your UserForm
    Please Login or Register  to view this content.
    The prescribe button overwrites previous entries, is this correct?

  18. #18
    Registered User
    Join Date
    08-18-2007
    Posts
    54
    Hi roy,


    Thanks for that change and yoru help.

    The prescribe button is meant to over write as i have yet to develop the function that clears the information after it has been exported.


    Many Thanks for your help
    Credit to this forum.

    Sam Williams

  19. #19
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Glad it helped, post back for further help.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1