Perplexing Combo Box

25 pts.
Tags:
ComboBox
VBA
I have a set of combo boxes that I would like to have January through December added to as options. I need all of them to be exactly the same. Any suggestions other than With cmbMonth1 .AddItem "January" .AddItem "February" End With With cmbMonth2 .AddItem "January" .AddItem "February" End With Thanks!
1

Answer Wiki

Thanks. We'll let you know when a new response is added.

I assume this is for Excel..?

1) Assign an array to the Combo Box’s List Property.

<pre>
Dim arrData As Variant

arrData = Array(“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”)

cmbMonth1.List = arrData
</pre>

2) Use the GetCustomListContents Method. You only need one line of code:

<pre>
cmbMonth1.List = Application.GetCustomListContents(4)
</pre>

The month names are stored as a custom list in Excel. I believe the full month names are loaded as a custom list by default in Excel. Go to Tools>Options>Custom Lists and look for “January, February, March” etc. In my copy of Excel 2003 it is the fourth entry, hence the number 4 as the argument for GetCustomListContents.

And I just read the part where you need to have 12 duplicate textboxes. So you’d need to list the above 12 times. i.e.

<pre>
cmbMonth1.List = Application.GetCustomListContents(4)
cmbMonth2.List = Application.GetCustomListContents(4)
cmbMonth3.List = Application.GetCustomListContents(4)
cmbMonth4.List = Application.GetCustomListContents(4)

etc
</pre>

–JP

You did not mention what programming language are you working with, but by your example, I will assume that it is VB.

I would use an array, and a for loop.

Something like this:

Define the array:
<pre>Dim months(12) As String</pre>

Initialize the array:
<pre>months(1) = “January”
months(2) = “February”
months(3) = “March”
months(4) = “April”

months(12) = “December”</pre>

Add the items to the combo boxes:
<pre>Dim i As Integer
For i = 1 To 12
Combo1.AddItem (months(i))
Combo2.AddItem (months(i))

ComboN.AddItem (months(i))
Next i
End Sub</pre>

Discuss This Question: 2  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.
  • Label1cc
    Oh... by the way, this is in VBA
    25 pointsBadges:
    report
  • Label1cc
    Never thought of that... Thank you very much!
    25 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.

Following

Share this item with your network: