Search code examples
excelvbatextboxlistboxpopulate

How to add a value from a text box to list box five times instantly?


I like to add textbox4.value to the List box 5 times with one click of a button. If textbox value is yes, i want the list box to list yes yes yes yes yes. I don't know it this is possible but i hope so.

Private Sub CommandButton2_Click()
If Me.TextBox4.Value = "" Then
Exit Sub
Else
ListBox1.AddItem Me.TextBox4.Value
Me.TextBox4.Value = ""
End If
Me.TextBox4.SetFocus
End Sub

Solution

  • Try:

    Private Sub CommandButton2_Click()
    
    If Me.TextBox4.Value = "" Then
        Exit Sub
    Else
        Dim i As Long
        For i = 1 To 5
            ListBox1.AddItem Me.TextBox4.Value
        Next i
        Me.TextBox4.Value = ""
    End If
    Me.TextBox4.SetFocus
    
    End Sub