Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I simplify my code? 4

Status
Not open for further replies.

JensKKK

Technical User
May 8, 2007
119
GB
I am almost sure that there is a better way of doing adding information into a combobox.

Any help regarding this problem is appreciated.
Thanks


UserForm2.ComboBox6.AddItem " 0 "
UserForm2.ComboBox6.AddItem " 1 "
UserForm2.ComboBox6.AddItem " 2 "
UserForm2.ComboBox6.AddItem " 3 "
UserForm2.ComboBox6.AddItem " 4 "
UserForm2.ComboBox6.AddItem " 5 "
UserForm2.ComboBox6.AddItem " 6 "
UserForm2.ComboBox6.AddItem " 7 "
UserForm2.ComboBox6.AddItem " 8 "
UserForm2.ComboBox6.AddItem " 9 "
UserForm2.ComboBox6.AddItem " 10 "
UserForm2.ComboBox6.AddItem " 11 "
UserForm2.ComboBox6.AddItem " 12 "
UserForm2.ComboBox6.AddItem " 13 "
UserForm2.ComboBox6.AddItem " 14 "
UserForm2.ComboBox6.AddItem " 15 "
UserForm2.ComboBox6.AddItem " 16 "
UserForm2.ComboBox6.AddItem " 17 "
UserForm2.ComboBox6.AddItem " 18 "
UserForm2.ComboBox6.AddItem " 19 "
UserForm2.ComboBox6.AddItem " 20 "
UserForm2.ComboBox6.AddItem " 21 "
UserForm2.ComboBox6.AddItem " 22 "
UserForm2.ComboBox6.AddItem " 23 "
UserForm2.ComboBox6.AddItem " 24 "
UserForm2.ComboBox6.AddItem " 25 "
UserForm2.ComboBox6.AddItem " 26 "
UserForm2.ComboBox6.AddItem " 27 "
UserForm2.ComboBox6.AddItem " 28 "
UserForm2.ComboBox6.AddItem " 29 "
UserForm2.ComboBox6.AddItem " 30 "
UserForm2.ComboBox6.AddItem " 31 "
UserForm2.ComboBox6.AddItem " 32 "
UserForm2.ComboBox6.AddItem " 33 "
UserForm2.ComboBox6.AddItem " 34 "
UserForm2.ComboBox6.AddItem " 35 "
UserForm2.ComboBox6.AddItem " 36 "
UserForm2.ComboBox6.AddItem " 37 "
UserForm2.ComboBox6.AddItem " 38 "
UserForm2.ComboBox6.AddItem " 39 "
UserForm2.ComboBox6.AddItem " 40 "
 
Code:
    With ComboBox1
        .List = Array("Blue", "Green", "Red")
    End With

Chance,

Filmmaker, gentleman and ROMAN!
 
Or also in your case

Code:
For X = 1 to 60 

ComboBox1.additem X 

next



Chance,

Filmmaker, gentleman and ROMAN!
 
for x = 1 to 40
UserForm2.ComboBox6.AddItem " " & str(x) & " "
next x

should work...

Uncle Mike
 
Thanks guys.

The obvious next question is how can I fill several comboboxes with information.

Is there a chance to build a loop around the 12 comboboxes?



'initialize combobox 1 & 12 with values
For i = 1 To 40
UserForm2.ComboBox1.AddItem i
Next

For i = 0 To 40
UserForm2.ComboBox2.AddItem i
Next

For i = 1 To 40
UserForm2.ComboBox3.AddItem i
Next

For i = 0 To 40
UserForm2.ComboBox4.AddItem i
Next

For i = 0 To 40
UserForm2.ComboBox5.AddItem i
Next

For i = 0 To 40
UserForm2.ComboBox6.AddItem i
Next

For i = 0 To 40
UserForm2.ComboBox7.AddItem i
Next

For i = 0 To 40
UserForm2.ComboBox8.AddItem i
Next

For i = 0 To 40
UserForm2.ComboBox9.AddItem i
Next

For i = 0 To 40
UserForm2.ComboBox10.AddItem i
Next

For i = 0 To 40
UserForm2.ComboBox11.AddItem i
Next

For i = 0 To 40
UserForm2.ComboBox12.AddItem i
Next
 
Perhaps this ?
For i = 1 To 12
For j = 0 To 40
UserForm2.Controls("ComboBox" & i).AddItem j
Next
Next

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
This is a nice little trick

("ComboBox" & i).

Thanks PHV
 
Or if you want to populate all the combobox, a variation from the FAQ could be.
Code:
Sub CommandButton1_Click()
Dim cCont As Control
Dim j As Long
  For Each cCont In Me.Controls
     If TypeOf cCont Is ComboBox Then
        With Me.Controls(cCont.Name)
           .Clear
           .AddItem "Please select a number."
           For j = 1 To 40
              .AddItem j
           Next
           .ListIndex = 0
         End With
     End If
   Next
End Sub
The reason this may be better is if you are properly naming the comboboxes. Ahem. Not just leaving them named "Combobox". This will action the comboboxes, regardless of name.

It is a good practice to explicitly name things. Writing code becomes saner (and easier to debug) if you can use (for example):
Code:
If cboPartNumber.Text = 34 Then
' yadda
instead of
Code:
If Combobox9.Text = 34 Then
  'yadda
Comboboxx does not really tell you anything.


It is also good practice the use .Clear before adding items. Remember it IS an additive process. The loop will add items to whatever is there. So if you have logic where you are changing the listed items - say you have some other process that puts 1 - 30 (not 1 - 40), then you want to clear it first.

It is also good practice (not certainly not required) to give an instruction on the first item, and display it with ListIndex = 0. Otherwise the combobox shows as blank. You could of course not use the item "Please select a number.". You could just use ListIndex = 0, in which case "1" would be visible.

NOTE: using:

TypeName(cCont) = "ComboBox"

vs

TypeOf cCont Is ComboBox

is mostly a matter of preference. The first is a string, the second is an object reference


faq219-2884

Gerry
My paintings and sculpture
 
Gery,

your comments are very helpful.

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top