Setting up a simple loop to fill labels?

J

justchris

Hi... been a while since i've posted here so Welcome back me!

I got a question for you all, I have about 30 labels on a userform an
I want my userform, on initilize, to fill those labels with values fro
the spreadsheet.

I'm hoping I can set up a small loop to do this and I guess my rea
question is How can I loop through 30 labels?

Would probably look like:

Dim LblName as object
dim i as integer


set lblName = userform.controls("label" & LblNumber)

for i = 1 to 30
lblname.*caption* = worksheets("sheet1").cells(1, *i*).value
next i



Any help would be great.. I have a lot of labels and a loop will mos
deffinatly help here
 
R

Rowan

Maybe:

Private Sub UserForm_Initialize()
Dim lbl As Object
Dim i As Integer
i = 1
With Sheets("Sheet1")
For Each lbl In Me.Controls
If TypeName(lbl) = "Label" Then
lbl.Caption = .Cells(i, 1).Value
i = i + 1
End If
Next lbl
End With
End Sub

Hope this helps
Rowan
 
J

justchris

Ok, Im not sure what to do with this.. :rolleyes:

I have 10 cells on sheet1 a1:a10 that need to go into labels1 - 10

i have 15 cells on sheet1 c10:c25 that need to go into labels 11 -26

and so on. How can i change the code above to fit those needs
 
R

Rowan

How about:

Private Sub UserForm_Initialize()
Dim lbl As Object
Dim i As Integer
i = 1
With Sheets("Sheet1")
For Each lbl In Me.Controls
If TypeName(lbl) = "Label" Then
If i < 11 Then
lbl.Caption = .Cells(i, 1).Value
Else
lbl.Caption = .Cells(i - 1, 3).Value
End If
i = i + 1
End If
Next lbl
End With
End Sub

Regards
Rowan
 
J

justchris

I found this bit of code... this is what i was lookin for, thanks though
Rowan.

UserForm1.Controls("Label" & i).Caption =

This Works!
 

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

Top