title: VBA: How to use a textbox to search a listbox, select multiple strings, and store selected strings in an Array
body: <p>I want to apologize ahead of time for how bad at coding i am.
  The objective is to create a userform where the user can use the text box to search for movie titles and they will appear in the listbox (enabled multiselect) where the user can select up to multiple movie titles. When this is done they can hit the done button which will compile said strings in an array and exit. I also have a cancel button which can just exit the userform, do i need this?</p>
  <p>Here is the code i have thus far, thank you tons</p>
  <pre><code>Option Explicit
  
  Private Sub cancelbutton_Click()
  
  End Sub
  
  Private Sub donebutton_Click()
  
  End Sub
  
  Private Sub ListBox1_Click()
  
  End Sub
  Private Sub TextBox1_Change()
   Dim j As Long, testString As String
   testString = &quot;*&quot; &amp; TextBox1.text &amp; &quot;*&quot;
   With Me.ListBox1
   .Clear
   .List = Sheets(&quot;HERS Data&quot;).Range(&quot;A2:J&quot; &amp; Sheets(&quot;HERS Data&quot;).Cells(Rows.Count, 1).End(xlUp).Row).Value
   If .listIndex = -1 And Len(TextBox1.text) Then
   For j = .ListCount - 1 To 0 Step -1
   If (Not (LCase(.List(j, 0)) Like testString) And (Not (LCase(.List(j, 1)) Like testString))) _
   And (Not (LCase(.List(j, 2)) Like testString) And (Not (LCase(.List(j, 3)) Like testString))) Then .RemoveItem j
   Next j
   End If
   End With
  End Sub
  Private Sub ListBox_AddFromArray(ByRef ComboBox As Object, ListArray As Variant)
      ListBox.Clear
      If IsArray(ListArray) Then
          Dim i As Long
          For i = LBound(ListArray) To UBound(ListArray)
              ListBox.AddItem ListArray(i), ListBox.ListCount
          Next i
      Else
          ListBox.AddItem ListArray, 0
      End If
  End Sub
  Private Sub CreateArrayFromListbox()
    Dim nIndex As Integer
    Dim arWords() As Variant
  
    ' dimension the array first instead of using 'preserve' in the loop
    ReDim arWords(ListBox1.ListCount - 1)
    
    For nIndex = 0 To ListBox1.ListCount - 1
      arWords(nIndex) = ListBox1.List(nIndex)
    Next
  End Sub
  
  </code></pre>
  <p>Also I have this line in here and I know it does not belong, I know it is using sheets but that does not apply to me as I already Have stored a .txt file (tab delmited) in an array. Sorry hang with me here</p>
  <pre><code>.List = Sheets(&quot;HERS Data&quot;).Range(&quot;A2:J&quot; &amp; Sheets(&quot;HERS Data&quot;).Cells(Rows.Count, 1).End(xlUp).Row).Value
  </code></pre>
  <p>The tab delimited data that I want the user to search from i have stored away in a array</p>
  <p>Also, I have this function that calls on the user form in my main code</p>
  <pre><code>Function UserInputForm() As Variant()
      Load UserForm1
      ListBox_AddFromArray UserForm1.ListBox1, arrH
      UserForm1.Show
      UserInputForm = UserForm1.arWords
      Unload UserForm1
  End Function
  </code></pre>
  <p>Where arrH has the list of 1000 movie titles i need and arWords is the string list array i wish to take away from it (user selected movies).</p>