create controls in userform dynamically; address userform without name

RAYLWARD102

Well-known Member
Joined
May 27, 2010
Messages
529
Already know how to do this by using the user form name
Example of a user form named u1:
Code:
Set LAB = u1.Controls.Add("Forms.Label.1")

I know how to create controls the above way, on demand dynamically (by directly referencing the user form name)

I'm trying to add controls dynamically, without explicitly referencing the user form name.
An error occurs when I go to set the .height of the label.

Example:
Code:
    For x = 0 To UserForms.Count - 1
        If LCase(UserForms.Item(x).Name) = LCase("u1") Then
        
            UserForms.Item(x).Controls.Add ("Forms.Label.1")
            With LAB
                 .Height = 10
            End With
        End If
    Next

Basically, I have some code that I want to use in various named userforms and was trying to build a function that would build out controls to a defined userform
rather than duplicating code for each of my various named user forms.
Basically, I do not want to address the user form by it's name when creating controls; I want address the userform by variable name
 
Last edited:

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
kind of solved my own problem
Code:
Dim MyForm As MSForms.UserForm
MyForm = u1

Set LAB = MyForm.Controls.Add("Forms.Label.1")
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,922
Messages
6,175,384
Members
452,639
Latest member
RMH2024

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