ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   UserForm Controls Collection (https://www.excelbanter.com/excel-programming/385490-userform-controls-collection.html)

Spencer

UserForm Controls Collection
 
Is there a way to "capture" all of the controls on a form that are of a
certain type. for example. Change the forecolor of all labels to Blue.

with my naming conventions i could probably do this with some kind of loop
since all my labels' names start with "lab", but i dont know how to identify
them in the controls collection as a specific type of control.

RB Smissaert

UserForm Controls Collection
 
Dim Ctl As MSForms.Control

For Each Ctl In Form1.Controls
If TypeName(Ctl) = "Label" Then
'control code here
End If
Next Ctl

RBS

"Spencer" wrote in message
...
Is there a way to "capture" all of the controls on a form that are of a
certain type. for example. Change the forecolor of all labels to Blue.

with my naming conventions i could probably do this with some kind of loop
since all my labels' names start with "lab", but i dont know how to
identify
them in the controls collection as a specific type of control.



Tom Ogilvy

UserForm Controls Collection
 
Another way:

Dim Ctl As MSForms.Control

For Each Ctl In Userform1.Controls
If TypeOf Ctl is MsForms.Label Then
'control code here
msgbox Ctl.name
End If
Next Ctl


Your original suggestion

for i = 0 to Userform1.controls.count - 1
if instr(1, me.Controls(i).name, "lab", vbTextCompare) = 1 then
msgbox me.controls(i).name
end if
Next


--
Regards,
Tom Ogilvy


"RB Smissaert" wrote in message
...
Dim Ctl As MSForms.Control

For Each Ctl In Form1.Controls
If TypeName(Ctl) = "Label" Then
'control code here
End If
Next Ctl

RBS

"Spencer" wrote in message
...
Is there a way to "capture" all of the controls on a form that are of a
certain type. for example. Change the forecolor of all labels to Blue.

with my naming conventions i could probably do this with some kind of
loop
since all my labels' names start with "lab", but i dont know how to
identify
them in the controls collection as a specific type of control.






All times are GMT +1. The time now is 02:45 PM.

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