Solved

Excel VBA - programatically design a user form

Posted on 2016-11-03
9
32 Views
Last Modified: 2016-11-04
Is it possible to place controls on a User Form at design time programatically?

Reason: to avoid having to manually name and align controls when there's a lot of them.
0
Comment
Question by:hindersaliva
  • 5
  • 2
  • 2
9 Comments
 
LVL 45

Assisted Solution

by:Martin Liss
Martin Liss earned 250 total points
Comment Utility
Yes it is, but the easiest thing to do is to put all the controls on the form that you'll ever need making all except the ones that you want to see at the start hidden.
0
 
LVL 45

Assisted Solution

by:Martin Liss
Martin Liss earned 250 total points
Comment Utility
To do it dynamically you do something like this which assumes you have a userform with a textbox named txtOriginal and a command button named CommandButton1.

Private Sub CommandButton1_Click()
Controls.Add "Forms.Textbox.1", "txtNew", True
With Controls("txtNew")
    .Top = txtOriginal.Top + txtOriginal.Height + 20
    .Height = txtOriginal.Height
    .Width = txtOriginal.Width
    .Left = txtOriginal.Left
End With

End Sub

Open in new window

0
 
LVL 45

Assisted Solution

by:Martin Liss
Martin Liss earned 250 total points
Comment Utility
If you want to add events to the created controls it's a little more complicated, so let me know.
0
 
LVL 17

Accepted Solution

by:
Roy_Cox earned 250 total points
Comment Utility
This example adds a userform with TextBoxes and 4 buttons with their code. You can add other controls as you want.
Dynamic-UserForm.xlsm
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:hindersaliva
Comment Utility
Roy, that's perfect :)
Thanks Martin. It helped too.
0
 
LVL 17

Expert Comment

by:Roy_Cox
Comment Utility
Pleased to help
0
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
You're welcome and I'm glad I was able to help.

If you expand the “Full Biography” section of my profile you'll find links to some articles I've written that may interest you.

Marty - Microsoft MVP 2009 to 2016
              Experts Exchange MVE 2015
              Experts Exchange Top Expert Visual Basic Classic 2012 to 2015
0
 

Author Comment

by:hindersaliva
Comment Utility
Some really good stuff there Martin. Thanks.
0
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
Thanks.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now