Search code examples
excelvbatextboxuserform

How to set text dynamically to texbox in vba?


Dear Fellow StackOverFlow-members,

I try to dynamically create texboxes in a userform and set default text depending on value from a tab.

First the user fill in textboxes like in the photo
first_step
When he clicks the button above, it calls the function that create textboxes in the same userform. Exactly as much as the total of values of the textboxes, futhermore for value "0" it counts as 1 textbox to add.

What I want is when it encounters 0 value I want the new texbox created to set defaut text as "null". Like so :
result_expected

My code :

For i = 0 To UBound(tabValTextBox)
            valTemp = tabValTextBox(i)
            If valTemp = 0 Then
                iTextBoxMasqueA = iTextBoxMasqueA + 1
                Set textBoxCableA = UserForm1.Controls.Add("Forms.TextBox.1")
                colTextBoxCableA.Add textBoxCableA
                With textBoxCableA
                    .Name = "cable" & iTextBoxCableA
                    .Top = iTextBoxCableA * textBoxCableA.Height + 50
                    .Left = 150
                    .Text = "Nul"
                End With
            Else
                For j = 0 To valTemp - 1
                    iTextBoxCableA = iTextBoxCableA + 1
                    Set textBoxCableA = UserForm1.Controls.Add("Forms.TextBox.1")
                    colTextBoxCableA.Add textBoxCableA
                    With textBoxCableA
                        .Name = "cable" & iTextBoxCableA
                        .Top = iTextBoxCableA * textBoxCableA.Height + 50
                        .Left = 150
                    End With
                Next j

            End If
        Next i


tabValTextbox() is a tab containing the values of the left textboxes.
However the result I get looks like this :
result_reached
I don't get as much textboxes as expected.

I don't understand where I'm missing something. I want to learn from this, so if possible, explain me what I'm doing wrong here, or if my approach is lacking insight.


Solution

  • Try this:

    For i = 0 To UBound(tabValTextBox)
        valTemp = tabValTextBox(i)
        iTextBoxCableA = iTextBoxCableA + 1
        If valTemp = 0 Then
            Set textBoxCableA = UserForm1.Controls.Add("Forms.TextBox.1")
            colTextBoxCableA.Add textBoxCableA
            With textBoxCableA
                .Name = "cable" & iTextBoxCableA
                .Top = iTextBoxCableA * textBoxCableA.Height + 50
                .Left = 150
                .Text = "Nul"
            End With
        Else
            For j = 0 To valTemp - 1
                Set textBoxCableA = UserForm1.Controls.Add("Forms.TextBox.1")
                colTextBoxCableA.Add textBoxCableA
                With textBoxCableA
                    .Name = "cable" & iTextBoxCableA
                    .Top = iTextBoxCableA * textBoxCableA.Height + 50
                    .Left = 150
                End With
            Next j
        End If
    Next i
    

    I've moved the iTextBoxCableA increment outside of the zero condition, and removed the iTextBoxMasqueA increment entirely as you weren't using it.

    I can't test this myself but I think it'll sort the problem of overlapping out.