ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Userform Controls (https://www.excelbanter.com/excel-programming/319816-userform-controls.html)

Nigel

Userform Controls
 
Hi All
I have a set of controls on a userform, named x1, x2, x3 ........... xn etc

Is there a way of setting these controls in a loop eg

for i = 1 to n
set control x of i to something
next i

I am not sure how to point to a named control by constructing a string for
the name? Help!

--
Cheers
Nigel





Bob Phillips[_6_]

Userform Controls
 
Nigel,

As long as you have a structured naming convention, you could use code like
this that gets the value from each textbox on the form

Dim ctl As Control

For Each ctl In Me.Controls
If TypeName(ctl) = "TextBox" Then
MsgBox ctl.Text
End If
Next ctl

--

HTH

RP
(remove nothere from the email address if mailing direct)


"Nigel" wrote in message
...
Hi All
I have a set of controls on a userform, named x1, x2, x3 ........... xn

etc

Is there a way of setting these controls in a loop eg

for i = 1 to n
set control x of i to something
next i

I am not sure how to point to a named control by constructing a string for
the name? Help!

--
Cheers
Nigel







Tushar Mehta[_8_]

Userform Controls
 
To answer your specific question, you could use Controls("X" & i).

You may also find John Walkenbach's object-oriented solution useful. See
Handle Multiple UserForm Buttons With One Subroutine
http://j-walk.com/ss/excel/tips/tip44.htm

"Nigel" wrote:

Hi All
I have a set of controls on a userform, named x1, x2, x3 ........... xn etc

Is there a way of setting these controls in a loop eg

for i = 1 to n
set control x of i to something
next i

I am not sure how to point to a named control by constructing a string for
the name? Help!

--
Cheers
Nigel






Nigel

Userform Controls
 
Thanks Bob, that is part of the problem solved. My challenge is with a
subset of controls of the same type I wish to act on.

I have a set of code linked to controls named c1 to cn, an event from anyone
of these acts on another control named x1, x2 to xn etc.

Whichever control c1 to cn is pressed the same code for x1 to xn needs to
run. Somehow I need to pass the number of the cn control to the code to act
on the xn control.

Any thoughts?


--
Cheers
Nigel



"Bob Phillips" wrote in message
...
Nigel,

As long as you have a structured naming convention, you could use code

like
this that gets the value from each textbox on the form

Dim ctl As Control

For Each ctl In Me.Controls
If TypeName(ctl) = "TextBox" Then
MsgBox ctl.Text
End If
Next ctl

--

HTH

RP
(remove nothere from the email address if mailing direct)


"Nigel" wrote in message
...
Hi All
I have a set of controls on a userform, named x1, x2, x3 ........... xn

etc

Is there a way of setting these controls in a loop eg

for i = 1 to n
set control x of i to something
next i

I am not sure how to point to a named control by constructing a string

for
the name? Help!

--
Cheers
Nigel









Nigel

Userform Controls
 
Thanks, most helpful.

--
Cheers
Nigel



"Tushar Mehta" wrote in message
...
To answer your specific question, you could use Controls("X" & i).

You may also find John Walkenbach's object-oriented solution useful. See
Handle Multiple UserForm Buttons With One Subroutine
http://j-walk.com/ss/excel/tips/tip44.htm

"Nigel" wrote:

Hi All
I have a set of controls on a userform, named x1, x2, x3 ........... xn

etc

Is there a way of setting these controls in a loop eg

for i = 1 to n
set control x of i to something
next i

I am not sure how to point to a named control by constructing a string

for
the name? Help!

--
Cheers
Nigel








Nigel

Userform Controls
 
Ok, Tushar Mehta has provide answer in sub thread.

Controls("name")


--
Cheers
Nigel



"Nigel" wrote in message
...
Thanks Bob, that is part of the problem solved. My challenge is with a
subset of controls of the same type I wish to act on.

I have a set of code linked to controls named c1 to cn, an event from

anyone
of these acts on another control named x1, x2 to xn etc.

Whichever control c1 to cn is pressed the same code for x1 to xn needs to
run. Somehow I need to pass the number of the cn control to the code to

act
on the xn control.

Any thoughts?


--
Cheers
Nigel



"Bob Phillips" wrote in message
...
Nigel,

As long as you have a structured naming convention, you could use code

like
this that gets the value from each textbox on the form

Dim ctl As Control

For Each ctl In Me.Controls
If TypeName(ctl) = "TextBox" Then
MsgBox ctl.Text
End If
Next ctl

--

HTH

RP
(remove nothere from the email address if mailing direct)


"Nigel" wrote in message
...
Hi All
I have a set of controls on a userform, named x1, x2, x3 ...........

xn
etc

Is there a way of setting these controls in a loop eg

for i = 1 to n
set control x of i to something
next i

I am not sure how to point to a named control by constructing a string

for
the name? Help!

--
Cheers
Nigel












All times are GMT +1. The time now is 06:01 AM.

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