Using indexing on controlsin a form

SiddersAtCHEP

Board Regular
Joined
Nov 2, 2005
Messages
104
I have a form which has several textboxes. Depending on choices made in a listbox, I need to fill one, some or all of the textboxes with information. I do not want to have to refer to them as TextBox1, TextBox2 etc. I would prefer to use something like:

for i = 1 to 10
textBox(i) = activecell.value
activecell.offset(1,0).select
next i

In effect I want an array of controls that I can refer to by their index number. I have tried naming the controls TextBox(1) etc. but I keep getting an error when I try this.

Any suggestions?
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.

Fat Cat

Active Member
Joined
Nov 5, 2004
Messages
336
There's a couple of ways of doing it.

To do all the textbox controls on a Form you could;

Code:
For Each ctl In Me.Controls
    If TypeOf ctl Is MSForms.TextBox Then
        ctl.Text = True
    End If
Next ctl

To just do specific textboxes you can use the following;

Code:
Me.Controls(1).Text = "Box 1"
Me.Controls(2).Text = "Box 2"
Me.Controls(3).Text = "Box 3"

Where Me.Controls(1) is the control on the Form that has its TABINDEX property set to 1

Or in a loop;

Code:
For i=1 To 3
     Me.Controls(i).Text = "Box " & i
Next i
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,652
Messages
5,832,928
Members
430,175
Latest member
Sheenamarie

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top