Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

VBA Visual Basic for Applications (Microsoft) FAQ

VBA How To

Create User Form Using VBA
Posted: 16 Mar 05 (Edited 29 Nov 05)

Sometimes you need to create a complex UserForm with many components. I had to create a form with 3 labels, 1 text box and a check box for 43 different records (215 separate components) and so developed this technique of programatically creating the form.

Obviously one can manually create a complex UserForm but the problem of individually naming each component and writing the event handler codes is a real drag.

The references that need to be loaded are:
  Visual Basic For Applications
  Microsoft Excel Object Library
  MS Forms 2.0 Object Library

The code below is based on John Walkenbach's original procedure.

CODE

Sub MakeUserForm()
    Dim TempForm As Object
    Dim NewButton As MSForms.commandbutton
    Dim NewLabel As MSForms.Label
    Dim NewTextBox As MSForms.TextBox
    Dim NewOptionButton As MSForms.OptionButton
    Dim NewCheckBox As MSForms.CheckBox
    Dim X As Integer
    Dim Line As Integer
    Dim MyScript(4) As String

    'This is to stop screen flashing while creating form
    Application.VBE.MainWindow.Visible = False

    Set TempForm = ThisWorkbook.VBProject.VBComponents.Add(3)

    'Create the User Form
    With TempForm
        .Properties("Caption") = "My User Form"
        .Properties("Width") = 450
        .Properties("Height") = 300
    End With

    'Create 10 Labels
    For X = 0 To 9
        Set NewLabel = TempForm.designer.Controls.Add("Forms.label.1")
        With NewLabel
            .Name = "FieldLabel" & X + 1
            .Caption = "My Label " & X + 1
            .Top = 20 + (12 * X)
            .Left = 6
            .Width = 90
            .Height = 12
            .Font.Size = 7
            .Font.Name = "Tahoma"
            .BackColor = &H80FFFF
        End With
    Next

    'Create 10 Text Boxes
    For X = 0 To 9
        Set NewTextBox = TempForm.designer.Controls.Add("Forms.textbox.1")
        With NewTextBox
            .Name = "MyTextBox" & X + 1
            .Top = 20 + (12 * X)
            .Left = 100
            .Width = 150
            .Height = 12
            .Font.Size = 7
            .Font.Name = "Tahoma"
            .BorderStyle = fmBorderStyleSingle
            .SpecialEffect = fmSpecialEffectFlat
        End With
    Next

    'Create 10 Check Boxes
    For X = 0 To 9
        Set NewCheckBox = TempForm.designer.Controls.Add("Forms.checkbox.1")
        With NewCheckBox
            .Name = "MyCheck" & X + 1
            .Caption = ""
            .Top = 20 + (12 * X)
            .Left = 260
            .Width = 12
            .Height = 12
            .Font.Size = 7
            .Font.Name = "Tahoma"
            .BackColor = &HFF00&
        End With
    Next

    'Create 10 Labels -> result of Check Box
    For X = 0 To 9
        Set NewLabel = TempForm.designer.Controls.Add("Forms.label.1")
        With NewLabel
            .Name = "Result_Text" & X + 1
            .Caption = ""
            .Top = 20 + (12 * X)
            .Left = 280
            .Width = 150
            .Height = 12
            .Font.Size = 7
            .Font.Name = "Tahoma"
            .BackColor = &H80FFFF
        End With
    Next

    'Create Event Handler Code For Each Check Box
    '(True -> Upper Case of Text Box Value;False -> Lower Case of Text Box Value)
    For X = 0 To 9
        With TempForm.codemodule
            Line = .countoflines
            MyScript(0) = "Sub MyCheck" & X + 1 & "_Click()"
            MyScript(1) = "If .MyCheck" & X + 1 & " = true then"
            MyScript(2) = ".result_Text" & X + 1 & ".caption = ucase(.mytextbox" & X + 1 & ".value)"
            MyScript(3) = ".result_Text" & X + 1 & ".caption = lcase(.mytextbox" & X + 1 & ".value)"
            .insertlines Line + 3, MyScript(0)
            .insertlines Line + 2, "With Me"
            .insertlines Line + 3, MyScript(1)
            .insertlines Line + 4, MyScript(2)
            .insertlines Line + 5, "Else"
            .insertlines Line + 6, MyScript(3)
            .insertlines Line + 7, "End if"
            .insertlines Line + 8, "End With"
            .insertlines Line + 9, "End Sub"
        End With
    Next

    'Show the form
    VBA.UserForms.Add(TempForm.Name).Show

    'Delete the form (Optional)
    'ThisWorkbook.VBProject.VBComponents.Remove TempForm

End Sub

I tend not to delete the form but build up a series of UserForms (I refer to them as TestForms) - one generated each time the code is run. In this way I can check the results of any changes I made and use the latest TestForm to manually move components, get their new positions and modify the code accordingly.

When satisfied I can rename the final TestForm to an appropriate name, add any other components such as command buttons and other event handler codes, and then delete all the remailing TestForms.

This code can also be used to create a user form on the fly but command buttons and their event handlers must be added in the code.

Back to VBA Visual Basic for Applications (Microsoft) FAQ Index
Back to VBA Visual Basic for Applications (Microsoft) Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close