Referencing dynamically created UserForm Controls

stone_house

New Member
Joined
Jun 3, 2015
Messages
2
Hello,

This is my first post, so I apologize in advance if the format or content is irregular.

I created a Userform in designer mode, inside which controls need to be added or deleted as necessary. The reason is due to the highly variable amount of data that may need to be entered (it could be as few as 1, or as many as 60). I have so far been successful in dynamically adding controls, and providing them with names, etc., but then I'm entirely unsure as to how to reference those newly added controls.

I even checked the object browser, under my UserForm, and none of the controls that were added in run-time were listed there.

So I guess my question is two-fold: where are these objects stored? How do I reference them? Following is a greatly simplified version of what I'm trying to do:

Code:
'This is code used to add the control, which is initiated by SpinUp of a SpinButton

Private Sub Chg_Entry_SpinUp()
Dim TopHeight As Long
TopHeight = 240 + (Val(Chg_Entry.Value) - 1) * 54
With Me.Controls.Add("Forms.TextBox.1", "NewEntry")
        .Height = 18
        .Left = 18
        .Top = TopHeight
        .Width = 90
End With

'That works just fine; however, when I try to remove "NewEntry", for instance, I run into problems. This is what I tried, with the SpinDown of the same SpinButton:

Private Sub Chg_Entry_SpinDown()
Me.Controls.Remove ("NewEntry")
End Sub

'I've tried referencing "NewEntry" in a number of different syntaxes, to no avail; including:

'Me.Controls("NewEntry")
'Me!NewEntry
'Me![NewEntry]
'Me("NewEntry")

In every case I get a Run-time error saying "Could not find the specified object."

Any help would be greatly appreciated.

Thanks,
Aaron
 
Last edited:

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
I figured it out, at least for now. It may not be an elegant solution, as I have to use this line for every object I want to work with:

Code:
With Me
       .Controls("NewEntry").SetFocus
End With

After which, I can work with the object created at run-time.
 
Upvote 0

Forum statistics

Threads
1,214,956
Messages
6,122,465
Members
449,085
Latest member
ExcelError

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