how to use a variable in with me.label(x)

smartphreak

Board Regular
Joined
Mar 24, 2011
Messages
52
Hey guys,

I am trying to create a dynamic form where I loop through the variable and create labels (and ultimately cbo) using the variable.
VBA Code:
Do While d >= dx
With Me.Label(dx)
    .Caption = "How many task in Phase " & dx & "?"
    .Width = 150
    .Height = 30
    .Left = 5
    .Top = 10 * dx + 30 * (dx - 1)
End With

dx = dx + 1

Loop

I get a Compile error... Any thoughts?
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Something like this

VBA Code:
Dim ctl As Control

For Each ctl In Me.Controls
    If TypeName(ctrl) = "Label" Then
        <your code here>
    End If
Next ctl
 
Upvote 0
@Zot . i think the OP is looking for dynamic labels...

this may be the sort of code you are looking for (Note, these labels require extra code to enable events)
this example should help

VBA Code:
    Do While d >= dx
        Set But = Controls.Add("Forms.Label.1", "MyLabel" & dx, True)
        But.Left = 5
        But.Height = 30
        But.Width = 150
        But.Top = 10 * dx + 30 * (dx - 1)
        But.Caption = "How many task in Phase " & dx & "?"
        But.TabStop = False
    Loop
 
Upvote 0
If you are trying to adjust labels that you have already created, and they are named Label1, Label2 etc, you can use:

Code:
For dx = 1 to d
With Me.Controls("Label" & dx)
    .Caption = "How many task in Phase " & dx & "?"
    .Width = 150
    .Height = 30
    .Left = 5
    .Top = 10 * dx + 30 * (dx - 1)
End With

Next dx
 
Upvote 0
Solution
plenty of choices now :)
@RoryA would it be prudent to DoEvents after the caption update?
 
Upvote 0
@Zot . i think the OP is looking for dynamic labels...

this may be the sort of code you are looking for (Note, these labels require extra code to enable events)
this example should help

VBA Code:
    Do While d >= dx
        Set But = Controls.Add("Forms.Label.1", "MyLabel" & dx, True)
        But.Left = 5
        But.Height = 30
        But.Width = 150
        But.Top = 10 * dx + 30 * (dx - 1)
        But.Caption = "How many task in Phase " & dx & "?"
        But.TabStop = False
    Loop
Aha ... I read title me.label(x) and misunderstood :(
 
Upvote 0
It shouldn't really be necessary, but I suppose you could add a Me.Repaint line after the loop.
 
Upvote 0

Forum statistics

Threads
1,214,951
Messages
6,122,446
Members
449,083
Latest member
Ava19

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top