Markus,
Here is the code revised so that multiple column of textboxes can be created. Like the previous code Userform1 must have a textbox called Textbox1 and a button called CommandButton1. Userform2 can be blank or have any objects you please as long as they are not named Textbox1, TextBox2, etc..
**NEW**
when you launch Userform1, in Textbox1 enter the number of total textboxes, a "/", and the number of columns (eg. 34/4) which yield 34 total textboxes in 4 columns. The code auto-arranges the remainder of the textboxes. The height and width of the form are automatically calculated using the number of columns, width, and height of the textboxes.
HTH,
Maud
Add Textboxes.png
Private Sub CommandButton1_Click()
'===================================
'CODE TO ADD TEXTBOX(s) WITH PARAMETERS
'FROM USERFORM1 TO USERFORM2
'===================================
Dim tbox As Control, numcol()
Dim boxLeft As Integer, boxTop As Integer
Dim boxHeight As Long, boxWidth As Long
Dim I As Integer, J As Integer, index As Integer
Dim num As Integer, cols As Integer, remainder As Integer
num = Val(Split(TextBox1.Value, "/")(0)) 'NUMBER OF TEXTBOXES
cols = Val(Split(TextBox1.Value, "/")(1)) 'NUMBER OF COLUMNS
remainder = num Mod cols 'REMAINDER OF TEXTBOXES/COLUMNS
num = Int(num / cols) 'NUMBER OF TEXTBOXES/COLUMNS- ROUND TO INTEGER
'-----------------------------------
'EDITABLE PARAMETERS- THESE CAN BE CHANGED TO SUIT NEEDS
boxLeft = 50 'LEFT COLUMN MARGIN INTERVAL
boxTop = 50 'START OF FIRST ROW OF TEXTBOXES
interval = 20 'ROWS INTERVALS FROM START
boxHeight = 16.2 'HEIGHT OF TEXT BOXES (MUST BE LESS THAN INTERVAL)
boxWidth = 40 'WIDTH OF TEXTBOXES (MUST BE LESS THEN BOXLEFT INTEVAL)
'-----------------------------------
'NON-EDITABLE PARAMETERS- DO NOT CHANGE
index = 1 'COUNTER FOR TEXTBOX NAME
ReDim numcol(cols + 1) 'SIZES ARRAY OF NUMBER OF COLUMNS +1
'-----------------------------------
'CREATE COLUMNS OF TEXTBOXWS
For I = 1 To cols
'-----------------------------------
'DETERMINE NUMBER OF TEXTBOXES IN EACH COLUMN INCLUDING REMAINDER
If remainder > 0 Then
numcol(I) = num + 1:: remainder = remainder - 1
Else
numcol(I) = num
End If
'-----------------------------------
'ADD TEXTBOX AND SET TEXTBOX PARAMETERS
For J = 1 To numcol(I)
Set tbox = UserForm2.Controls.Add("Forms.TextBox.1") 'CREATE INSTANCE OF TEXTBOX
tbox.Name = "TextBox" & index:: index = index + 1
tbox.Left = boxLeft * I
tbox.Top = boxTop:: boxTop = boxTop + interval
tbox.Height = boxHeight
tbox.Width = boxWidth
tbox.Object.Value = ""
Set tbox = Nothing
Next J
boxTop = 50 'SET TO THE SAME AS BOXTOP
Next I
'-----------------------------------
'ADJUST WIDTH AND HEIGHT OF USERFORM2 THEN DISPLAY
UserForm2.Width = boxLeft * (cols + 2)
UserForm2.Height = (interval * numcol(1)) + (boxTop * 2)
UserForm2.Show
Unload Me
End Sub
Bookmarks