referencing forms controls

  • Thread starter Thread starter AlbertYWang
  • Start date Start date
A

AlbertYWang

I have a series of labels on a form that I want to change the text
color on programatically.
If the label names are like 'label1', 'label2','label3', is there a a
way to loop through them?

Albert Wang
 
You can loop through them as members of the Controls collection

Dim ctl As Control

For Each ctl In Me.Controls

If ctl.Name = "Label2" Then
Me.Label2.ForeColor = vbRed
End If
Next ctl
 
some added info:

Dim ctl As Control

For Each ctl In Me.Controls
if typeof ctl is MSForms.Label then
Me.Label2.ForeColor = vbGreen
End If
Next ctl

or

for i = 1 to 5
me.controls("Label" & i).ForeColor = RGB(0,255,0)
Next
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top