Create Userform on the Fly?

PA HS Teacher

Well-known Member
Joined
Jul 17, 2004
Messages
2,838
Is it possible to create a userform on the fly?

I know how to create controls on the fly.
e.g.
Set ctl = MyForm.Controls.Add("forms.CommandButton.1")

But what if I wanted to create MyForm on the fly?
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Maybe, you can customize a skeleton UF that is already in the add-in...

Is it possible to create a userform on the fly?

I know how to create controls on the fly.
e.g.
Set ctl = MyForm.Controls.Add("forms.CommandButton.1")

But what if I wanted to create MyForm on the fly?
 
Upvote 0
Gene,
I've take a look at j-walk's site. I also found this Dave Hawley Post.
http://www.ozgrid.com/forum/showthread.php?t=16809&highlight=userform+fly

All of the suggestions I've read appear to require access to the VBA project during runtime. I generally don't think this is a good idea, and I always keep it locked.

I think I will take up Tusharm's suggestion of making generic userforms ahead of time. I think I can just make more forms than I anticipate needing at runtime.
 
Upvote 0
Another way that will not demand that security settings be lowered (Trust Access to Visual Basic Project must be checked/enabled for JWalks method to work) is to simply create a template userform and then create an instance of it. If you have very similiar forms, you can use this method to combine them into one or to combine events for multiple forms.

TemplateUserform.zip

Create a new workbook.
Create a userform named, "MyTemplateForm".
Placew this code in some class module such as ThisWorkbook or a Worksheet class...

<table border="1" bgcolor="White"><caption ALIGN=left><font size="2" face=Courier New>Example VBA Code:</FONT></caption><tr><td><font size="2" face=Courier New>  <font color="#0000A0">Public</font> <font color="#0000A0">WithEvents</font> uf <font color="#0000A0">As</font> UserForm
  <font color="#0000A0">Private</font> mtf <font color="#0000A0">As</font> MyTemplateForm
  
  <font color="#0000A0">Sub</font> Example()
       <font color="#0000A0">Set</font> mtf = <font color="#0000A0">New</font> MyTemplateForm
       <font color="#0000A0">Set</font> uf = mtf
       <font color="#0000A0">Dim</font> l <font color="#0000A0">As</font> MSForms.Label
      
       mtf.Caption = "Instance of MyTemplateForm"
       <font color="#0000A0">Set</font> l = mtf.Controls.Add("Forms.Label.1", "Description", True)
           l.Caption = "This is a simple blank form. I can do whatever " & _
                      "I wish via code... Click the form!"
           l.AutoSize = True
       mtf.Show
       Unload mtf
       <font color="#0000A0">Set</font> mtf = <font color="#0000A0">Nothing</font>
       <font color="#0000A0">Set</font> uf = <font color="#0000A0">Nothing</font>
  <font color="#0000A0">End</font> <font color="#0000A0">Sub</font>
  
  <font color="#0000A0">Private</font> <font color="#0000A0">Sub</font> uf_MouseDown(ByVal Button <font color="#0000A0">As</font> Integer, <font color="#0000A0">ByVal</font> Shift <font color="#0000A0">As</font> Integer, <font color="#0000A0">ByVal</font> X <font color="#0000A0">As</font> Single, <font color="#0000A0">ByVal</font> Y <font color="#0000A0">As</font> Single)
       <font color="#0000A0">With</font> mtf.Controls("Description")
           .Left = X
           .Top = Y
       <font color="#0000A0">End</font> <font color="#0000A0">With</font>
  <font color="#0000A0">End</font> <font color="#0000A0">Sub</font>
  
  
  <font color="#0000A0">Private</font> <font color="#0000A0">Sub</font> uf_MouseMove(ByVal Button <font color="#0000A0">As</font> Integer, <font color="#0000A0">ByVal</font> Shift <font color="#0000A0">As</font> Integer, <font color="#0000A0">ByVal</font> X <font color="#0000A0">As</font> Single, <font color="#0000A0">ByVal</font> Y <font color="#0000A0">As</font> Single)
       mtf.Caption = X & ", " & Y
  <font color="#0000A0">End</font> <font color="#0000A0">Sub</font>
</FONT></td></tr></table>

TemplateUserform.zip
 
Upvote 0
{snip}
<table border="1" bgcolor="White"><caption ALIGN=left><font size="2" face=Courier New>Example VBA Code:</FONT></caption><tr><td><font size="2" face=Courier New>  <font color="#0000A0">Public</font> <font color="#0000A0">WithEvents</font> uf <font color="#0000A0">As</font> UserForm
  <font color="#0000A0">Private</font> mtf <font color="#0000A0">As</font> MyTemplateForm
{snip}</td></tr></table>
{snip}
That's an interesting way to solve the problem of userform not exporting critical methods such as Show [Of course, I am not sure I understand why it doesn't but that's another story. {grin}] and MyTemplateForm not having any events attached to it [which is yet another story.]
 
Upvote 0
Tusharm. Why does this not work? The custom Initialize event is not fired...

Template code.

Code:
Option Explicit

Event Initialize()

Private Sub UserForm_Initialize()
    RaiseEvent Initialize
End Sub

In class module.
Code:
Option Explicit

Private WithEvents uf As UserForm
Private WithEvents mtf As MyTemplateForm

Sub Example()
    Set mtf = New MyTemplateForm
    Set uf = mtf
    Dim l As MSForms.Label
    
    mtf.Caption = "Instance of MyTemplateForm"
    Set l = mtf.Controls.Add("Forms.Label.1", "Description", True)
        l.Caption = "This is a simple blank form.  I can do whatever " & _
                   "I wish via code...  Click the form!"
        l.AutoSize = True
    mtf.Show
    Unload mtf
    Set mtf = Nothing
    Set uf = Nothing
End Sub

Private Sub mtf_Initialize()

End Sub

Private Sub uf_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    With mtf.Controls("Description")
        .Left = X
        .Top = Y
    End With
End Sub


Private Sub uf_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    mtf.Caption = X & ",  " & Y
End Sub
 
Upvote 0
Nevermind. When I renamed the event, it fires...

Template code:
Code:
Option Explicit

Public Event Initialize2()

Private Sub UserForm_Initialize()
    RaiseEvent Initialize2
End Sub

Code in sheet1 class:
Code:
Option Explicit

Private WithEvents uf As UserForm
Private WithEvents mtf As MyTemplateForm

Sub Example()
    Set mtf = New MyTemplateForm
    Set uf = mtf
    Dim l As MSForms.Label
    
    mtf.Caption = "Instance of MyTemplateForm"
    Set l = mtf.Controls.Add("Forms.Label.1", "Description", True)
        l.Caption = "This is a simple blank form.  I can do whatever " & _
                   "I wish via code...  Click the form!"
        l.AutoSize = True
    mtf.Show
    Unload mtf
    Set mtf = Nothing
    Set uf = Nothing
End Sub

Private Sub mtf_Initialize2()
Stop
End Sub

Private Sub uf_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    With mtf.Controls("Description")
        .Left = X
        .Top = Y
    End With
End Sub


Private Sub uf_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    mtf.Caption = X & ",  " & Y
End Sub
 
Upvote 0
Right Click,
Thank you for your post. It looks like exactly what I want, and I appreciate you posting an example file, but I'm not quite sure how to create more than one instance at runtime. I don't understand why your code does not have its own class module, and I"m not sure how to create multiple instances.

Background:
I am working on a Lesson Planning system, basically a database. I've created my own objects using class modules to help me manage my data. (Forgive me if my language is sloppy. I'm relatively new at creating my own objects) I've also created some collections of my objects.

My "highest" object successfuly contains the following "properties." I've had success manipulating them and adding them to a collection.
- Range Objects (Cells)
- A collection of Range objects
- Regular Variable types, integer, string, etc.
- Controls, TextBoxes, Command Buttons etc. on an existing form


Question(s):
1. Is it possible to have a Userform as part of this object?
2. Can these forms be loaded and left in the background simultaneously?
3. Can I collect up my records of my new object, including the UF propery into a Collection.

I anticipate something on the order of 10 records/objects, and hence around 10 forms. I will not know exactly how many records until runtime.
 
Upvote 0
Is it possible to create a userform on the fly?
I am in the process of developing my web site, and in the mean time have a very modest temporary single web page.

On it is a downloadable example at the bottom that is a userform being created programmatically, in this example for the purpose of entering an unambiguous date.

It also includes a verification of, and message box instructions for, the user's trusted source setting to be established, because this code writes to the VBE.

Also on the page is a photo of some guy with a face made for radio.

http://www.atlaspm.com/pages/1/index.htm
 
Upvote 0

Forum statistics

Threads
1,215,334
Messages
6,124,319
Members
449,154
Latest member
pollardxlsm

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