use looping to update label

  • Thread starter Thread starter leungkong
  • Start date Start date
L

leungkong

i want to use looping to update the label but i cannot. (as below code)
Thanks.

for i = 1 to 10
label(i).visible = false
next i
 
Hi Leungkong,
There is no "label" collection.
I assume that your labels are on a userform.
You need to loop throught the Controls collection of your Userform and
by using the .Item property. This uses base 0. So to loop through 10
labels your loop should be from 0 to 9. Then test each control to see
if it is a label. If it is then do whatever you want.
You could also user the Userform.Controls.Count property so you dont
have to change your loop if you add more labels.
Hope this is what you were looking for.

Kind regards

Bernie Russell

-------------------------------------------------------------------
Private Sub UpdateLabels()

On Error GoTo Err_UpdateLabels
Dim i As Integer
With UserForm1.Controls
For i = 0 To 9 'Alternatively Userform.Controls.Count -1
If (TypeOf .Item(i) Is MSForms.Label) = True Then .Item(i).Visible =
False
'If (TypeOf .Item(i) Is MSForms.Label) = True Then .Item(i).Visible
= True
Next i
End With
Err_UpdateLabels:
Debug.Print Err.Description
End Sub
 
If you named the labels nicely, you could rely on their names:

I named mine label1, label2, label3, ..., label10

Dim iCtr As Long
For iCtr = 1 To 10
Me.Controls("Label" & iCtr).Visible = False
Next iCtr


If you didn't know the number of labels, but still wanted to hide them all, you
could use:

Dim ctrl As Control
For Each ctrl In Me.Controls
If TypeOf ctrl Is MSForms.Label Then
ctrl.Visible = False
End If
Next ctrl
 

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