Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Labels for a dialog box 1

Status
Not open for further replies.

Cage

MIS
Aug 25, 2002
50
CA
I have several labels which I use in a dialog box, I want to for loop through these labels and change them.

for i = 1 to 100
UserForm1.label(i).Caption = vc(i)
next i

ie change the label caption to the value in my array vc().

Does anyone know how I can do this, there are 100 values in vc() and do not want to have to write out

userform1.label1.caption = vc(1)
userform1.label2.caption = vc(2)
and also on.....

TIA

Cage
 
If all your label names started with Label and you wanted to change all of them then you could use:

Code:
Dim CurrControl As Control
    
For Each CurrControl In UserForm1.Controls
   If UCase(Left(CurrControl.Name, 5)) = "LABEL" Then
      CurrControl.Object.Caption = vc(i)
   End If
Next


If all your label names started with Label and you wanted to change the ones that were 100 or less, as in Label85 then you could use:

Code:
Dim CurrControl As Control
 
For Each CurrControl In UserForm1.Controls
   On Error Resume Next
   If UCase(Left(CurrControl.Name, 5)) = "LABEL" And _
      CInt(Mid(CurrControl.Name, 6)) <= 100 Then
      If Err.Number = 0 Then
         CurrControl.Object.Caption = vc(i)
      Else
         Err.Clear
      End If
   End If
Next
 
Try
Code:
Dim oCtrl As Control
Dim i As Integer
'dim vc()
'populate vc()
For Each oCtrl In UserForm1.Controls
    If TypeName(oCtrl) = &quot;Label&quot; Then
        i = i + 1
        oCtrl = vc(i)
    End If
Next

A.C.

 
Thanks acron. Apparently I haven't had enough coffee yet. I forgot to diminsion the index and increment it. Also I couldn't remember the TypeName function, so had to go the long way around.

A star for you.

 
thanks a lot, this problem has been bugging me for ages.

Cage
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top