How to Create Text Boxes on User Form at Runtime

How to create text boxes on User Form at runtime, access the created text boxes, enter data into them and transfer the data to the worksheet. There are many complicated ways of doing this but we describe below an elegant and easy method. Watch the video:

 

Watch this video on YouTube.

Here’s the complete VBA code:

Dim number As Long
Private Sub UserForm_Initialize()
Dim i As Long
number = InputBox(“Enter no of text-boxes you wish to create at run-time”, “Enter TextBox Number”)
Dim txtB1 As Control
For i = 1 To number
Set txtB1 = Controls.Add(“Forms.TextBox.1”)
With txtB1
.Name = “txtBox” & i
.Height = 20
.Width = 150
.Left = 20
.Top = 20 * i * 1
End With
Next i
End Sub

Private Sub CommandButton1_Click()
Dim p As Long
For p = 1 To number
Cells(1, p) = Controls(“txtBox” & p).Text
‘Cells(p, 1) = Controls(“txtBox” & p).Text
Next p
End Sub

Private Sub CommandButton2_Click()
Unload Me
End Sub

Further reading:

How to create textboxes during runtime, and name them

Published by

Dinesh Kumar Takyar

Welcome to exceltrainingvideos.com! My aim is to help you learn MS Excel including VBA. I always felt that a good way to learn something was to find solutions to problems in that domain. That is why I share these Excel videos with you. Mostly these are questions asked during my corporate training sessions. I've been training individuals and companies in computers since 1991. I did my B. Sc. (Vordiplom), M. Sc. (Diplom) and Ph.D. (Dr. rer. nat.) from Hamburg, Germany. The best thing about solving some of my visitor's questions is that I also gain new insights. For more Excel VBA solutions you may like to visit my YouTube channel: http://youtube.com/familycomputerclub For a structured Excel VBA training course online you can visit: https://www.youtube.com/excelvbaonline

2 thoughts on “How to Create Text Boxes on User Form at Runtime”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.