Solved

Populate Combobox Values at a click of button

Posted on 2011-03-05
4
634 Views
Last Modified: 2012-05-11
I want to populate the Combobox with the list of predefined values declared in another sheet in same excel at a click of command button



0
Comment
Question by:srikanthv2322
  • 2
  • 2
4 Comments
 
LVL 30

Accepted Solution

by:
SiddharthRout earned 500 total points
ID: 35044804
Please amend as applicable.

Private Sub CommandButton1_Click()
    '~~> This will populate from Row 1 to Row 10
    '~~> from Col A in Sheet 2
    For i = 1 To 10
        ComboBox1.AddItem Sheets("Sheet2").Range("A" & i).Value
    Next
End Sub

Open in new window


Sid
0
 
LVL 14

Expert Comment

by:Zack Barresse
ID: 35045024
I would amend Sid's code to include a clear functionality, which will not make your list "double" when you click the button a second time...
Private Sub CommandButton1_Click()
    Dim i As Long
    ComboBox1.Clear
    For i = 1 To 10
        ComboBox1.AddItem Sheets("Sheet2").Range("A" & i).Value
    Next
End Sub

Open in new window

Or, you can use another method, one I prefer, to enter the data all at once instead of looping (I hate looping)...
Private Sub CommandButton1_Click()
    ComboBox1.Clear
    ComboBox1.List = Sheets("Sheet2").Range("A1:A10").Value
End Sub

Open in new window

HTH
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35045028
Thanks Zack

I missed on "ComboBox1.Clear". Nice catch!

Yeah, I completely agree with you on "Looping".

Sid
0
 
LVL 14

Expert Comment

by:Zack Barresse
ID: 35045054
Just another way to skin the cat.  I'm not really sure what the OP's end-game is with this particular query, which is why I posted the secondary routine.  It may in fact be a loop is what is called for - I have no idea! :)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question