ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Enumerate form items (https://www.excelbanter.com/excel-programming/333030-enumerate-form-items.html)

Kurt[_7_]

Enumerate form items
 
How can I enumerate form items so that I can, say, change BackColor,
checkbox status, etc. in a loop?

or, as an alternative, if I generate the object name ("form1.cb1",
"form1.cb2", etc) as a string, how can I refer to it using the string?

Thanks,

....kurt



Bob Phillips[_7_]

Enumerate form items
 

Dim ctl As Control

For Each ctl In Me.Controls
Select Case TypeName(ctl)
Case "CheckBox": MsgBox "checkbox " & ctl.Name
Case "CommandButton": MsgBox "Button " & ctl.Name
End Select
Next ctl


--
HTH

Bob Phillips

"Kurt" wrote in message
...
How can I enumerate form items so that I can, say, change BackColor,
checkbox status, etc. in a loop?

or, as an alternative, if I generate the object name ("form1.cb1",
"form1.cb2", etc) as a string, how can I refer to it using the string?

Thanks,

...kurt





Tom Ogilvy

Enumerate form items
 
for i = 1 to 2
set cbx = form1.controls("cb" & i)
Next


or
sStr = "cb1"

set cbx = form1.Controls(sStr)
--
Regards,
Tom Ogilvy


"Kurt" wrote in message
...
How can I enumerate form items so that I can, say, change BackColor,
checkbox status, etc. in a loop?

or, as an alternative, if I generate the object name ("form1.cb1",
"form1.cb2", etc) as a string, how can I refer to it using the string?

Thanks,

...kurt





William Benson

Enumerate form items
 
Is this a userform, or a worksheet? I have written routines which loop
through control objects on userforms as follows:

Dim ctrl as Control
For Each ctrl In Me.Controls
If ctrl.Tag = "SpecialItem" Then

'Do things to all items I have set the Tag property a certain
way for (i.e., = 'SpecialItem' or whatever)
End if
'OR
If LCASE(left(ctrl.Name,3)) = "ckb" then
'Do things to items I have named with prefix 'ckb'
End If
'OR
If TypeName(ctrl) = "CheckBox" then
'Do things to all checkboxes
End if
Next ctrl


Note: The second item - testing the name - does not work if you created the
control dynamically as a control array,
such as
Dim chk(1 to 3) As Control
Set chk(1) = Controls.Add("Forms.CheckBox.1", True)
Set chk(2) = Controls.Add("Forms.CheckBox.1", True)
Set chk(3) = Controls.Add("Forms.CheckBox.1", True)

because the name property for each will just be "-1"
"Kurt" wrote in message
...
How can I enumerate form items so that I can, say, change BackColor,
checkbox status, etc. in a loop?

or, as an alternative, if I generate the object name ("form1.cb1",
"form1.cb2", etc) as a string, how can I refer to it using the string?

Thanks,

...kurt




Kurt[_6_]

Enumerate form items
 
Thanks to all who replied. This is exactly what I was looking for. [:-)]

....kurt


"Kurt" wrote in message
...
How can I enumerate form items so that I can, say, change BackColor,
checkbox status, etc. in a loop?

or, as an alternative, if I generate the object name ("form1.cb1",
"form1.cb2", etc) as a string, how can I refer to it using the string?

Thanks,

...kurt





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

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