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

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
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,213,528
Messages
6,114,154
Members
448,553
Latest member
slaytonpa

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