Solved

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

Posted on 2013-05-14
6
1,053 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

777 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