ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   How to refer to controls by string (https://www.excelbanter.com/excel-programming/289111-how-refer-controls-string.html)

Darren Hill[_2_]

How to refer to controls by string
 
Is there anyway to use a control's string name to manipulate it?
At the moment, I have a lot of controls named in a handy manner
("Combobox_A1", "ComboBox_B2", etc.)
I often need to be able to change the box based on the "A1" identifier.
At the moment, I'm using a loop as follows

For each Ctrl in MyForm.Controls
If (right(ctrl.name,2) = "A1" then
ctrl.value = whatever

But it would be nice to simply use the "A1", build the string "Combobox_A1"
and then say
Control("Combobox_a1").Value = whatever

What's the syntax for this?

Darren




Rob van Gelder[_4_]

How to refer to controls by string
 
Darren,

You were nearly there.

MyForm.Controls("Combobox_a1").Value = whatever

You're probably able to omit the MyForm.

Rob



"Darren Hill" wrote in message
...
Is there anyway to use a control's string name to manipulate it?
At the moment, I have a lot of controls named in a handy manner
("Combobox_A1", "ComboBox_B2", etc.)
I often need to be able to change the box based on the "A1" identifier.
At the moment, I'm using a loop as follows

For each Ctrl in MyForm.Controls
If (right(ctrl.name,2) = "A1" then
ctrl.value = whatever

But it would be nice to simply use the "A1", build the string

"Combobox_A1"
and then say
Control("Combobox_a1").Value = whatever

What's the syntax for this?

Darren






Tom Ogilvy

How to refer to controls by string
 
Put an "s" on Control
Userform1.Controls("Combobox_a1").Value

should work

mval = "A1"
Controls("Combobox_" & mval).Value = "ABCD"

would also be another example.

--
Regards,
Tom Ogilvy

Darren Hill wrote in message
...
Is there anyway to use a control's string name to manipulate it?
At the moment, I have a lot of controls named in a handy manner
("Combobox_A1", "ComboBox_B2", etc.)
I often need to be able to change the box based on the "A1" identifier.
At the moment, I'm using a loop as follows

For each Ctrl in MyForm.Controls
If (right(ctrl.name,2) = "A1" then
ctrl.value = whatever

But it would be nice to simply use the "A1", build the string

"Combobox_A1"
and then say
Control("Combobox_a1").Value = whatever

What's the syntax for this?

Darren






Darren Hill[_2_]

How to refer to controls by string
 
Thanks, Tom and Rob. I was pretty close wasn't I? )

--
Darren




All times are GMT +1. The time now is 04:28 PM.

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