Solved

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

Posted on 2013-05-14
6
994 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now