This should help. I've added textboxes to a second userform during run time when you click on one row in the first userform.
Here's the code for reference
Code:
Public var As Variant
Dim rng As Range
Private Sub ListBox1_Click()
UserForm2.Show 0
End Sub
Private Sub UserForm_Initialize()
With ListBox1
Set rng = Worksheets(Replace(Split(.RowSource, "!")(0), "'", "")).Range(Split(.RowSource, "!")(1))
'If your row source doesn't have reference to a sheet, but only to a range, just use Set rng = Range(RowSource)
var = rng
.RowSource = ""
.List = var
End With
End Sub
Private Sub UserForm_Terminate()
rng.Value = var
End Sub
Code:
Dim txtSampleTextBox() As MSForms.TextBox
Private Sub UserForm_Initialize()
Dim lng As Long
Dim obj As MSForms.TextBox
lng = UserForm1.ListBox1.ColumnCount
ReDim Preserve txtSampleTextBox(1 To lng)
For lng = 1 To lng
Set txtSampleTextBox(lng) = Controls.Add("Forms.TextBox.1", "txtSampleTextBox" & lng)
With txtSampleTextBox(lng)
.Left = (lng - 1) * .Width + 5
.Text = UserForm1.ListBox1.List(UserForm1.ListBox1.ListIndex, lng - 1)
End With
Next lng
Me.Width = (lng - 1) * (txtSampleTextBox(lng - 1).Width + 1)
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Dim lng As Long
With UserForm1.ListBox1
For lng = 1 To Me.Controls.Count
.List(.ListIndex, lng - 1) = Controls("txtSampleTextBox" & lng).Text
Next lng
UserForm1.var = .List
End With
End Sub
Bookmarks