Results 1 to 9 of 9

Thread: Creating a drop down list box in Excel (a beginner)

Hybrid View

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2006
    Posts
    4

    Creating a drop down list box in Excel (a beginner)

    I am a beginner of VBA. I searched the forum for the answer of my question but I have a hard time understanding the terminology in the posts.

    Basically, I just want to create a drop down list box in VBA. The topics that will appear in the list box are already fixed and will never ever change in the future. The user will pick a topic from the list and the code will take appropriate action accordingly. Can someone tell me how to do this in layman's terms? Or could you lead me to a reading?? I have been googling about this but couldn't find a tutorial.

    Thanks!

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Creating a drop down list box in Excel (a beginner)

    if it is a fixed list you can add the items to the listbox at design rather than in code, then just use code to do whatever depending on which item is selected
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Creating a drop down list box in Excel (a beginner)

    Why are you posting the same question again and again?



    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  4. #4

    Thread Starter
    New Member
    Join Date
    Nov 2006
    Posts
    4

    Re: Creating a drop down list box in Excel (a beginner)

    westconn1, thanks for your response. How can I add items to the listbox in "design rather than code". Thanks in advance!!

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Creating a drop down list box in Excel (a beginner)

    what program are you using listbox?
    and what version?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6
    Junior Member
    Join Date
    Nov 2006
    Posts
    25

    Re: Creating a drop down list box in Excel (a beginner)

    do you want to create the listbox / combobox on an excel worksheet or create it on a form?

    if its on a worksheet

    http://www.techonthenet.com/excel/qu...eate_combo.php

    otherwise,

    create a form within VBA name it form1 or what ever you want,

    create a combo box on the form

    name it:

    listBox1 or what ever you want


    Code:
    Private Sub form1_Activate()
    
       listBox1.AddItem "Item1"
       listBox1.AddItem "Item2"  
       listBox1.AddItem "Item3"     'etc
    
    End Sub
    this will run when the user changes the combo box

    Code:
    Private Sub listBox1_Change()
       
       Dim tempValue As String 
       
       tempValue=listBox1.Value
    
       If tempValue = "item1" Then
            'do this
       ElseIf tempValue = "item2" Then
            'do this
       Else
            'do this
       End If
    End Sub

    Hope this helps!

  7. #7
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Creating a drop down list box in Excel (a beginner)

    Hi I was just wondering if you need to create that in a worksheet then there is a simple way of doing it rather than using VB.

    Use Excel Validation

    Steps

    1) In any Column, type the name of the topics. Say in cell A1, A2, A3, A4, A5 type "apples", "oranges", "grapes", "guava" and "Melon" respectively

    2) select the cell where you want the list to populate say B1
    3)click on the menu data->validation
    4)Under the Settings tab select "lists" as the validation criteria
    5) click the little red arrow in the source "prompt"
    6) select the range i.e A1:A5 and press enter twice

    your list is ready! if you click on B1, you will see your list will drop down :-)
    Last edited by Siddharth Rout; Nov 14th, 2006 at 02:48 PM. Reason: spelling mistake
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  8. #8
    New Member
    Join Date
    May 2007
    Posts
    2

    Re: Creating a drop down list box in Excel (a beginner)

    that's a good idea koolsid

    I have a problem though, I have created a drop down list, however, it refers to a non-unique list, so I have duplicate options. It does not look really good, especially if I have a lot of multiple similar options. Due to the nature of the data, I can not force the source of entries only contain unique data.

    So, I am looking for a solution to generate a unique validation list (ie. neglecting duplicate entries). Any ideas?

    Thanks.

  9. #9
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Creating a drop down list box in Excel (a beginner)

    If you are refering to creating the list in Excel sheet then the best way to do it is create a pivot table (check the link in my signature) and then use that list for the validation... this way you will always get one entry rather then duplicate entries...

    In the layout part of the pivot table, keep the say 'names' field in the row and the 'count of names' in the data.

    Hope this helps...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

Posting Permissions

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



Click Here to Expand Forum to Full Width