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

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
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
 
Upvote 0

Forum statistics

Threads
1,214,650
Messages
6,120,736
Members
448,988
Latest member
BB_Unlv

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
Back
Top