ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Cycling through form fields (https://www.excelbanter.com/excel-programming/313329-cycling-through-form-fields.html)

Azza

Cycling through form fields
 
Help
I can use FOR statements with a range in a spreadsheet...

i.e: For Rownumber = 5 To EndOfList
'Gets the name and whether outstanding or not from the
spreadsheet
VName = Range("A" & Rownumber).Value
VOut = Range("J" & Rownumber).Value

But I want to know how to cycle through a set of comboboxes using a
WITH statement.

i.e: For x = 1 To 15

vBoxName = (cboxDay & x)

With vBoxName
.additem ("1")
.additem ("2")
...
End With
Next

I can't seem to get the syntax correct to fill the field cboxDay1,
then fill the box cboxDay2 ..... with the same info.

I don't want to have to right the same code out for all 45 boxes.

Any solution would be appreciated.
cheers
Aaron

keepITcool

Cycling through form fields
 
Try:


Private Sub UserForm_Initialize()
Dim asItm(2) As String, i As Integer
'Fill array iso using additem.
asItm(0) = "FirstItem"
asItm(1) = "SecondItem"
asItm(2) = "ThirdItem"

For i = 1 To 10
With Me.Controls("Combobox" & i)
.List = asItm
End With
Next


End Sub

keepITcool

< email : keepitcool chello nl (with @ and .)
< homepage: http://members.chello.nl/keepitcool


(Azza) wrote:

Help
I can use FOR statements with a range in a spreadsheet...

i.e: For Rownumber = 5 To EndOfList
'Gets the name and whether outstanding or not from the
spreadsheet
VName = Range("A" & Rownumber).Value
VOut = Range("J" & Rownumber).Value

But I want to know how to cycle through a set of comboboxes using a
WITH statement.

i.e: For x = 1 To 15

vBoxName = (cboxDay & x)

With vBoxName
.additem ("1")
.additem ("2")
...
End With
Next

I can't seem to get the syntax correct to fill the field cboxDay1,
then fill the box cboxDay2 ..... with the same info.

I don't want to have to right the same code out for all 45 boxes.

Any solution would be appreciated.
cheers
Aaron



Bob Phillips[_6_]

Cycling through form fields
 
How about this

Dim ctl As msforms.Control

For Each ctl In Me.Controls
If TypeName(ctl) = "ComboBox" Then
With ctl
.AddItem ("1")
.AddItem ("2")
' ...
End With
End If
Next ctl

--

HTH

RP

"Azza" wrote in message
m...
Help
I can use FOR statements with a range in a spreadsheet...

i.e: For Rownumber = 5 To EndOfList
'Gets the name and whether outstanding or not from the
spreadsheet
VName = Range("A" & Rownumber).Value
VOut = Range("J" & Rownumber).Value

But I want to know how to cycle through a set of comboboxes using a
WITH statement.

i.e: For x = 1 To 15

vBoxName = (cboxDay & x)

With vBoxName
.additem ("1")
.additem ("2")
...
End With
Next

I can't seem to get the syntax correct to fill the field cboxDay1,
then fill the box cboxDay2 ..... with the same info.

I don't want to have to right the same code out for all 45 boxes.

Any solution would be appreciated.
cheers
Aaron





All times are GMT +1. The time now is 04:31 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com