Looping UserForm ComboBox

  • Follow


Hi

I would like to use the code below in a loop so that i can populate numerous 
ComboBoxes within my UserForm with the same list.

I am not sure how to go about this, and would appreciate any help.

Thanks

With ComboBox7
    Dim MonthArray As Variant
    MonthArray = Split("01|02|03|04|05|06|07|08|09|10|11|12|", "|")
    ComboBox7.List = MonthArray
End With
0
Reply Utf 6/6/2010 8:36:37 AM

Something like:

Option Explicit
Private MonthArray As Variant
Private Sub UserForm_Initialize()
    MonthArray = Split("01|02|03|04|05|06|07|08|09|10|11|12|", "|")
    Me.ComboBox1.list = MonthArray
    Me.ComboBox2.list = MonthArray
    Me.ComboBox3.list = MonthArray
    Me.ComboBox4.list = MonthArray
    Me.ComboBox5.list = MonthArray
End Sub

will do the job

-- 
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>


"Owen" <Owen@discussions.microsoft.com> wrote in message 
news:B4F28044-8A5D-464C-A43A-2696531ECF2C@microsoft.com...
> Hi
>
> I would like to use the code below in a loop so that i can populate 
> numerous
> ComboBoxes within my UserForm with the same list.
>
> I am not sure how to go about this, and would appreciate any help.
>
> Thanks
>
> With ComboBox7
>    Dim MonthArray As Variant
>    MonthArray = Split("01|02|03|04|05|06|07|08|09|10|11|12|", "|")
>    ComboBox7.List = MonthArray
> End With 


0
Reply Graham 6/6/2010 9:22:23 AM


Thanks Graham...it did the trick.

"Graham Mayor" wrote:

> Something like:
> 
> Option Explicit
> Private MonthArray As Variant
> Private Sub UserForm_Initialize()
>     MonthArray = Split("01|02|03|04|05|06|07|08|09|10|11|12|", "|")
>     Me.ComboBox1.list = MonthArray
>     Me.ComboBox2.list = MonthArray
>     Me.ComboBox3.list = MonthArray
>     Me.ComboBox4.list = MonthArray
>     Me.ComboBox5.list = MonthArray
> End Sub
> 
> will do the job
> 
> -- 
> <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
> Graham Mayor -  Word MVP
> 
> My web site www.gmayor.com
> Word MVP web site http://word.mvps.org
> <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
> 
> 
> "Owen" <Owen@discussions.microsoft.com> wrote in message 
> news:B4F28044-8A5D-464C-A43A-2696531ECF2C@microsoft.com...
> > Hi
> >
> > I would like to use the code below in a loop so that i can populate 
> > numerous
> > ComboBoxes within my UserForm with the same list.
> >
> > I am not sure how to go about this, and would appreciate any help.
> >
> > Thanks
> >
> > With ComboBox7
> >    Dim MonthArray As Variant
> >    MonthArray = Split("01|02|03|04|05|06|07|08|09|10|11|12|", "|")
> >    ComboBox7.List = MonthArray
> > End With 
> 
> 
> .
> 
0
Reply Utf 6/6/2010 11:59:19 AM

You are welcome :)

-- 
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>


"Owen" <Owen@discussions.microsoft.com> wrote in message 
news:4629F442-49B7-4252-BF2F-B30A7F06C681@microsoft.com...
> Thanks Graham...it did the trick.
>
> "Graham Mayor" wrote:
>
>> Something like:
>>
>> Option Explicit
>> Private MonthArray As Variant
>> Private Sub UserForm_Initialize()
>>     MonthArray = Split("01|02|03|04|05|06|07|08|09|10|11|12|", "|")
>>     Me.ComboBox1.list = MonthArray
>>     Me.ComboBox2.list = MonthArray
>>     Me.ComboBox3.list = MonthArray
>>     Me.ComboBox4.list = MonthArray
>>     Me.ComboBox5.list = MonthArray
>> End Sub
>>
>> will do the job
>>
>> -- 
>> <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
>> Graham Mayor -  Word MVP
>>
>> My web site www.gmayor.com
>> Word MVP web site http://word.mvps.org
>> <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
>>
>>
>> "Owen" <Owen@discussions.microsoft.com> wrote in message
>> news:B4F28044-8A5D-464C-A43A-2696531ECF2C@microsoft.com...
>> > Hi
>> >
>> > I would like to use the code below in a loop so that i can populate
>> > numerous
>> > ComboBoxes within my UserForm with the same list.
>> >
>> > I am not sure how to go about this, and would appreciate any help.
>> >
>> > Thanks
>> >
>> > With ComboBox7
>> >    Dim MonthArray As Variant
>> >    MonthArray = Split("01|02|03|04|05|06|07|08|09|10|11|12|", "|")
>> >    ComboBox7.List = MonthArray
>> > End With
>>
>>
>> .
>> 


0
Reply Graham 6/6/2010 12:25:16 PM

Graham's answer is will do the job quite well in this case.  There are a few
other techniques that you might put in your toolbox for cases where the
number of controls involved is large:

The first loops through like named controls with an index number:

Private MonthArray As Variant
Private Sub UserForm_Initialize()
Dim i As Long
Dim CtrNameArray As String
CtrNameArray = Split("ComboBox1|ComboBox1|ComboBox1|ComboBox1|ComboBox1")
MonthArray = Split("01|02|03|04|05|06|07|08|09|10|11|12|", "|")
For i = 1 To 5 'or to whatever number you need.
  Me.Controls("ComboBox" & i).List = MonthArray
Next i
End Sub

The other uses a second array to provide uniquely named controls to the loop:

Private Sub UserForm_Initialize()
Dim i As Long
Dim CtrNameArray() As String 'Variant
CtrNameArray = Split
("ComboBoxAlpha|ComboBoxBravo|ComboBoxCharlie|ComboBoxDelta|ComboBoxEcho",
"|")
MonthArray = Split("01|02|03|04|05|06|07|08|09|10|11|12|", "|")
For i = 0 To UBound(CtrNameArray)
  Me.Controls(CtrNameArray(i)).List = MonthArray
Next i
End Sub



Owen wrote:
>Hi
>
>I would like to use the code below in a loop so that i can populate numerous 
>ComboBoxes within my UserForm with the same list.
>
>I am not sure how to go about this, and would appreciate any help.
>
>Thanks
>
>With ComboBox7
>    Dim MonthArray As Variant
>    MonthArray = Split("01|02|03|04|05|06|07|08|09|10|11|12|", "|")
>    ComboBox7.List = MonthArray
>End With

-- 
Greg Maxey

Please visit my web site http://gregmaxey.mvps.org

Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.aspx/word-docmanagement/201006/1

0
Reply Greg_Maxey 6/6/2010 1:33:56 PM

4 Replies
700 Views

(page loaded in 0.124 seconds)


Reply: