Consulting

Results 1 to 4 of 4

Thread: Solved: string to be object name

  1. #1

    Solved: string to be object name

    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

    [VBA]
    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
    [/VBA]

    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

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Assuming userform labels

    [vba]

    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
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    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

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Works fine for me with userform labels, althoug I did spot a typo in your doe

    [vba]

    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
    [/vba]

    You should learn to use Option Explicit
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •