Solved

create a combobox at runtime and fill with data on a multipage userform Page tab

Posted on 2013-05-14
6
1,110 Views
Last Modified: 2013-05-14
vba excel 2010

What I have:
Userform =  userform1
Multipage control =  Pages 1 to 9

I found this code:

But this code does not place the combobox on a userform and specifically "Page9"  ?





Sub CreateComboBox1()
    With ActiveSheet.OLEObjects.Add(ClassType:="Forms.ComboBox.1", _
                Link:=False, DisplayAsIcon:=False, Left:=50, Top:=80, Width:=100, _
                Height:=15)
        With .Object
            .AddItem "Date"
            .AddItem "Player"
            .AddItem "Team"
            .AddItem "Goals"
            .AddItem "Number"
        End With
    End With
End Sub


Thanks
fordraiders
0
Comment
Question by:fordraiders
  • 3
  • 3
6 Comments
 
LVL 33

Accepted Solution

by:
Norie earned 500 total points
ID: 39165258
This code will add a combobox to Page9 of a multipage at runtime.
Private Sub UserForm_Initialize()
Dim cbo As MSForms.ComboBox

    Set cbo = Me.MultiPage1.Pages(8).Controls.Add("Forms.Combobox.1")
    cbo.Left = 10
    cbo.Top = 10
    cbo.List = Array("Date", "Player", "Team", "Goals", "Number")
    
End Sub

Open in new window

0
 
LVL 3

Author Comment

by:fordraiders
ID: 39165310
Thanks, The reason i was trying to find code using the
            .AddItem "Date"
            .AddItem "Player"
            .AddItem "Team"
            .AddItem "Goals"
            .AddItem "Number"

Is because I may have sometimes 50 items or more to add to the combobox ?

or maybe if i have to use another line, (using your code) how to multiline it.(to extend the array list to another line would be fine) ?


Also
imnorie, just a question...I noticed browsing through code that most show code using  Initialize event.  This can be done regardlesss from a command button or some event from another combobox ?

Thanks very much fordraiders
0
 
LVL 33

Expert Comment

by:Norie
ID: 39165337
You can change the code I posted to use AddItem.
With cbo
       .AddItem "Date"
       .AddItem "Player"
       .AddItem "Team"
       .AddItem "Goals"
       .AddItem "Number"
End With

Open in new window

By the way, where will the 50 (or whatever) items be coming from?

You could call the code from a command button click or some other event, I posted for the Initialize event because your title mentioned adding at runtime.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:fordraiders
ID: 39165449
By the way, where will the 50 (or whatever) items be coming from?
hard coding it...
0
 
LVL 33

Expert Comment

by:Norie
ID: 39165505
Oh, thought they might have been stored somewhere, eg a worksheet.

That would have made it a lot easier to populate the combobox and would avoid hard-coding.
0
 
LVL 3

Author Closing Comment

by:fordraiders
ID: 39165911
thanks very much
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

733 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