Loop through Labels and Text Boxes within module code

D

Dan Gardner

I am trying to change Label Captions and Text Box Text in a loop located in
my module. For example I have a form that has 40 labels. I want to change the
text in the first "m" number of labels. At first I was hopeing to use
something like the following

for i = 1 to m
UserForm1."Label" & i.Caption = worksheets("Sheet1").cells(3,i).value
Next i

Excell does not seam to want to consummate "Label" & i to Label1 and
recognize it as a object within UserForm1. At the suggestion of a buddy and
another posting I was hopeing to do it defining a collection of Label objects
with the following code, however the "Set" statement doesn't seam to be valid
for a collection.

Dim Labs() As New Collection

Dim labelcount As Integer
Dim ctl As Control

labelcount = 0
For Each ctl In UserForm1.Controls
If TypeName(ctl) = "Label" Then
labelcount = labelcount + 1
ReDim Preserve Labs(1 To labelcount)
Set Labs(labelcount) = ctl
End If
Next ctl

For i = 1 To m
Labs(i).Caption = "cool"
Next i

I would appreciate any help on the matter, the end less dead ends are
driving me to madness. I'm useing Office 2000 and am upgradeing to Office 2003
 
B

Bob Phillips

Dan,

Access via the Controls collection, like

UserForm1.Controls("Label" & i).Caption =
worksheets("Sheet1").cells(3,i).value

--

HTH

RP
(remove nothere from the email address if mailing direct)
 
D

Dan Gardner

Thanks your awsome, I've been pulling my hair out for weeks on this. and now
I feel like an idiot
 

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