Solved

Excel VBA - programatically design a user form

Posted on 2016-11-03
9
53 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
9 Comments
 
LVL 47

Assisted Solution

by:Martin Liss
Martin Liss earned 250 total points
ID: 41872827
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 47

Assisted Solution

by:Martin Liss
Martin Liss earned 250 total points
ID: 41872848
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 47

Assisted Solution

by:Martin Liss
Martin Liss earned 250 total points
ID: 41872856
If you want to add events to the created controls it's a little more complicated, so let me know.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Accepted Solution

by:
Roy_Cox earned 250 total points
ID: 41873463
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
 

Author Comment

by:hindersaliva
ID: 41873765
Roy, that's perfect :)
Thanks Martin. It helped too.
0
 
LVL 19

Expert Comment

by:Roy_Cox
ID: 41874312
Pleased to help
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 41874370
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
ID: 41874408
Some really good stuff there Martin. Thanks.
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 41874428
Thanks.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

737 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