在您的代码中,您将对新文本框的引用保存在nieuwtxtingredient
.
您可以将此引用保存在一个文本框数组中,然后读取每个文本框的名称和值。
这就是我建议修改代码的方式:
Dim aantalBoxes As Integer
Dim ingredientArray() As Control
Private Sub btnVoegToe_Click()
Dim aantalRecepten As Integer
Dim Teller As Integer
aantalRecepten = Cells(2, Columns.Count).End(xlToLeft).Column
Cells(2, aantalRecepten + 2).Value = Me.txtNaamRecept.Value
For Teller = 1 To aantalBoxes ' <-- starts at 1, formula below adjusted
Cells(2 + Teller, aantalRecepten + 2).Value = ingredientArray(aantalBoxes).Value
Next Teller
End Sub
Private Sub btnVolgendeIngredient_Click()
aantalBoxes=aantalBoxes + 1 ' <-- must calculate the total
ReDim Preserve ingredientArray(aantalBoxes) ' <-- you had this in the earlier version
Set nieuwtxtingredient = Me.Controls.Add("Forms.Textbox.1", "Ingredient", True)
With nieuwtxtingredient
.Width = Me.txtIngredient0.Width
.Height = Me.txtIngredient0.Height
.Left = Me.txtIngredient0.Left
.Top = Me.txtIngredient0.Top + 30 * aantalBoxes
.Name = "txtIngredient" + CStr(aantalBoxes)
End With
' first Textbox is numbered 1
set ingredientArray(aantalBoxes) = nieuwtxtingredient ' <-- you had this in the earlier version
End Sub