PDA

View Full Version : Solved: string to be object name



AmardeepD
02-18-2008, 08:14 AM
ok so i have got a series of objects named Lblnumel1 through to Lblnumel15 and I want to be able to cycle through these changing their caption properties using a loop.

So far I have


For num = 1 To 15 Step 1
Labelname = "Lblnumel" & num
fstcellnum = 20 + num
seccellname = 21 + num
labelname.caption= num & ".) " & Worksheets("Sheet1").Range("B" & fstcellnum).value & Nextline & (Worksheets("Sheet1").Range("B" & seccellnum).value 'what i want it to equal
Next num


It says it has a problem with labelname.caption (which i am not surprised by) but i was also wondering if Nextline was the correct code to move down a line in the label :S:help

Bob Phillips
02-18-2008, 09:04 AM
Assuming userform labels



With Worksheets("Sheet1")

For num = 1 To 15 Step 1

fstcellnum = 20 + num
seccellname = 21 + num
Controls("Lblnumel" & num).Caption = num & ".) " & _
.Range("B" & fstcellnum).Value & Nextline & _
.Range("B" & seccellnum).Value 'what i want it to equal
Next num
End With

AmardeepD
02-18-2008, 09:23 AM
Ok ive tried that and its come up with runtime error 1004: application-defined or object-defined error on the line



Controls("Lblnumel" & num).Caption = num & ".) " & _
.Range("B" & fstcellnum).Value & Nextline & _
.Range("B" & seccellnum).Value

Bob Phillips
02-18-2008, 09:38 AM
Works fine for me with userform labels, althoug I did spot a typo in your doe



With Worksheets("Sheet1")

For num = 1 To 15 Step 1

fstcellnum = 20 + num
seccellnum = 21 + num
Controls("Lblnumel" & num).Caption = num & ".) " & _
.Range("B" & fstcellnum).Value & Nextline & _
.Range("B" & seccellnum).Value 'what i want it to equal
Next num
End With


You should learn to use Option Explicit