Dynamically Change Label Caption In VBA

angsuman

New Member
Joined
Aug 19, 2015
Messages
30
Hello All,

I have created one user form which contains two combo boxes one frame called frame1 and one command button. Inside Fram1, I have 42 labels which are named from Label1 to Label42. From drop down of combo box, I select month and year. Based on these two values, caption of the label will change. I am using following code to change the caption of the lable.

HTML:
    Dim i                   As Integer    Dim labelCount          As Integer    Dim k                   As Integer    Dim lblName             As String

    For i = 1 To labelCount        lblName = "Label" & CStr(i)        Me.Controls(lblName).Caption = k        k = k + 1    Next

However code gives me following error and does not change label caption. I understand that label cannot be viewed from user form. Above code is in user form initialize event level.


HTML:
Run-time Error  -2147024809 (80070057)
Could not find the specified object


Please help to access the label which is under Frame1 which is under UserForm1


Thanks
Angsuman
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Angsuman

It shouldn't matter that the labels are in a frame, you should still be able to access them directly by their name via the Controls collection of the userform.

Have you checked the names of all the labels?

PS You don't actually need CStr.
 
Upvote 0
Thank you Norie for your response.

I opened the Userform object and then selected the label. On left hand side in properties window it shows "Label1" against (Name). This is exact copy paste from property box. One point to highlight here that under Properties I do not see these labels (I have Label1 to Label42) in drop down until I select one of the Label from Label1 to Label42.
 
Upvote 0

Forum statistics

Threads
1,214,652
Messages
6,120,746
Members
448,989
Latest member
mariah3

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