Controlling Objects via Name?

Dave Punky

Board Regular
Joined
Jan 7, 2010
Messages
133
Hi all,

I'm sure this is relatively simple but I'm having a few issues wrapping my head around it.

Basically I have a sub which I want to control several labels - the labels all share the same naming convention, for the most part it's just a number that changes. Basically I want my sub to take the number and determine the object (in this case a label) to manipulate.

So for example, I have 3 labels (named lbl_name1a, lbl_name2a and lbl_name3a) and I want my sub to basically control the label, however it needs to know what label to control.

Essentially I want to control the label by defining it by saying it's ("lbl_name" & number & "a") but I am failing somewhat in making this work - whether it be because I'm defining it incorrectly or I'm just missing something obvious. The plan is I'll control it using another variable defined as msforms.label but it's just telling it what form to control to begin with causing the problem.

Hopefully somebody out there knows where I'm going wrong!
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
I am not aware of any way to dynamically name an object in code, but you can wrap them in a function to call them dynamically:

Code:
Public Function GetMyLabel(Index As Long) As Object
    Select Case Index
        Case 1: Set GetMyLabel = lbl_name1a
        Case 2: Set GetMyLabel = lbl_name2a
        Case 3: Set GetMyLabel = lbl_name3a
    End Select
End Function
 
Upvote 0
If the label is on a sheet (not form) and was made with "Control Tool Box" then, here's a snippet of code that shows a way:

Code:
Sub test()
    i = 3
    Sheet1.Shapes("Label " & i & " a").Select
    Selection.Object.Caption = "New Text"
End Sub

I think you can modify this to fit your needs
 
Upvote 0
Hello,

Maybe this.

<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> UserForm_Initialize()<br>  <SPAN style="color:#00007F">Dim</SPAN> i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN><br>  i = 2<br>  Controls("lbl_name" & i & "a").Caption = "yo"<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0
Hi guys,

Thanks for the suggestions, after an additional bit of research I found you can use the Controls() property to essentially do what I want.

For example:
number = 1
Userform1.Controls("Label " & number & " a").Caption = "Hello!"

Does the same job as:
Userform1.Label1a.Caption = "Hello!"

Note: I totally didn't see meldoc's post before posting but that's the logic I've used!
 
Upvote 0
Just a little suggestion: Whilst I would leave the number at the end (being a bit simple-minded), regardless of where the number is, format it for more digits than likely. That is, if one might have more than ten controls, use Format and keep a leading zero.
 
Upvote 0
@GTO - Admittedly I only ever planned to have 3 controls using this particular method however I will bear that in mind going forward as I can invisage a situation where that might occurr - thanks for the tip!
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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