title: Excel VBA - How To Deselect A Previosuly Selected Item In A Listbox
body: <p>I have a userform (uf1_assess_sched) in my Excel VBA project that has a listbox (uf1_listbox3).</p>
  
  <p>When the user selects a single item in this listbox, a second userform (group_1) is opened allowing the user to enter information specific to the selection she made on the first userform. Should the user wish to abandon further entry on group_1, she can exit by clicking a commandbutton called Exit.</p>
  
  <p>Upon exiting, group_1 is unloaded, and uf1_assess_sched takes the forefront. The idea is to allow the user to select another item from uf1)listbox3. However, the selection she originally made is still selected.</p>
  
  <p>How do I deselect this previously made selection.</p>
  
  <p>I have tried:</p>
  
  <pre><code>With uf1_assess_sched
      .uf1_listbox3.listindex = -1
  End With
  </code></pre>
  
  <p>This is the most relevant I could find in any of my searches.</p>
  
  <p>Following Patrick's suggestion, with my limited understanding of Excel VBA, this is how I interpreted his instructions.</p>
  
  <pre><code>With uf1_assess_sched
      For i = 0 To .uf1_listbox3.ListCount - 1
          If .uf1_listbox3.Selected(i) = True Then
              .uf1_listbox3.Selected(i) = False
          End If
      Next i
  End With
  </code></pre>
  
  <p>This to regrettably didn't work. The code did find the true selection, but the entry remained selected in the listbox and also triggered the uf1_listbox3_Click event.</p>
  
  <p>I hope I am providing feedback in the appropriate way, by simply editing my original post. I'm not sure how to add code to a comment. StackOverflow is a new format for me, so doing my best.</p>
  
  <p>With the latest code so kindly provided by Patrick, I managed to get so far before I encountered an error. I made some adaptations to reflect userform and listbox names. I'm getting a "Method or data member not found. " error with the code in the second userform, group_1.</p>
  
  <pre><code>Private Sub exit1_Click()
  
  Dim ui2 As VbMsgBoxResult
  Dim lastrow As Long
  Dim i As Long
  
  If ws_vh.Range("E2") &gt; 0 Then 'unsaved info
      Me.Label34.Caption = "    Saving unsaved rental data."
      Me.Label34.BorderColor = RGB(50, 205, 50)
      lastrow = ws_rd.Cells(ws_rd.Rows.Count, "A").End(xlUp).Row
      ws_rd.Range("A3:FZ" &amp; lastrow).Sort key1:=ws_rd.Range("A3"), order1:=xlAscending, Header:=xlNo
      Application.DisplayAlerts = False
      ThisWorkbook.Save
      Application.DisplayAlerts = True
  
      Debug.Print Me.Name, "exit1_Click() called"
      uf1_assess_sched.ListBox3_DeSelect    '&lt;--- Error with ".Listbox3_DeSelect"
      Unload Me
  
      'Unload group_1
      'End
      Exit Sub
  End If
  
  If ws_vh.Range("B2") &gt; 0 Then   'Outstanding rentals?
      ui2 = MsgBox("You still have " &amp; ws_vh.Range("C2") &amp; " rentals with critical missing rental information." &amp; Chr(13) &amp; Chr(13) _
          &amp; "Active (Sports) rentals: " &amp; ws_vh.Range("B3") &amp; Chr(13) &amp; "Passive (Picnics) rentals: " &amp; ws_vh.Range("B4") &amp; Chr(13) &amp; Chr(13) _
          &amp; "Are you sure you wish to exit?", vbInformation + vbYesNo, "OUTSTANDING RENTAL INFORMATION")
      If ui2 = vbYes Then
          If ws_vh.Range("N4") &gt; 0 Then
              Me.Label34.Caption = "    Saving unsaved rental data."
              Me.Label34.BorderColor = RGB(50, 205, 50)
              lastrow = ws_rd.Cells(ws_rd.Rows.Count, "A").End(xlUp).Row
              ws_rd.Range("A3:FZ" &amp; lastrow).Sort key1:=ws_rd.Range("A3"), order1:=xlAscending, Header:=xlNo
              Application.DisplayAlerts = False
              ThisWorkbook.Save
              Application.DisplayAlerts = True
              Workbooks("Sports15c.xlsm").Activate
              mbEvents = False
  
              Debug.Print Me.Name, "exit1_Click() called"
              uf1_assess_sched.ListBox3_DeSelect    '&lt;--- Error with ".Listbox3_DeSelect"
              Unload Me
              Exit Sub
          Else
              Unload Me
              End
          End If
      Else
          Exit Sub
      End If
   End If
  
  Unload group_1
  End
  End Sub
  </code></pre>
  
  <p>I did put the subs ListBox1_DeSelect() and ListBoxDeSelect(oListBox As Object) in a separate module (perhaps that is the problem). </p>
  
  <p>Here is that code ...</p>
  
  <pre><code>Sub ListBox3_DeSelect()
  ListBoxDeSelect Me.uf1_listbox3
  End Sub
  
  Private Sub ListBoxDeSelect(oListBox As Object)
  Dim i As Long
  If TypeName(oListBox) &lt;&gt; "ListBox" Then Exit Sub
  bSkipEvent = True
  With oListBox
      For i = 0 To .ListCount - 1
          If .Selected(i) Then
              .Selected(i) = False
          End If
      Next
  End With
  bSkipEvent = False
  End Sub
  </code></pre>
  
  <hr>
  
  <p>Here is my most recent code (July 19th) ...</p>
  
  <p>USERFORM 1 - uf1_assess_sched (holds listbox for which which user makes selection)</p>
  
  <pre><code>Private Sub uf1_listbox3_Click()
      If mbEvents Then Exit Sub
      Debug.Print Me.Name, "uf1_listBox3_Click() called"
      If bSkipEvent Then Exit Sub
      With uf1_listbox3
          Debug.Print Me.Name, "uf1_listBox3_Click() ListIndex: " &amp; .ListIndex &amp; " (" &amp; .List(.ListIndex) &amp; ")"
          group_1.Show
          'UserForm2.TextBox1.Value = .List(.ListIndex) ' This won't have effect if UserForm2 is True on ShowModal
      End With
  End Sub
  </code></pre>
  
  <p>USERFORM 2 - group_1 (allows user to enter additional data based on the value selected in userform1. User my opt to abandon by pressing EXIT button (exit1))</p>
  
  <pre><code>Private Sub exit1_Click()
  
      Dim ui2 As VbMsgBoxResult
      Dim lastrow As Long
      Dim i As Long
  
      If ws_vh.Range("E2") &gt; 0 Then 'unsaved info
          Me.Label34.Caption = "    Saving unsaved rental data."
          Me.Label34.BorderColor = RGB(50, 205, 50)
          lastrow = ws_rd.Cells(ws_rd.Rows.Count, "A").End(xlUp).Row
          ws_rd.Range("A3:FZ" &amp; lastrow).Sort key1:=ws_rd.Range("A3"), order1:=xlAscending, Header:=xlNo
          Application.DisplayAlerts = False
          ThisWorkbook.Save
          Application.DisplayAlerts = True
          Unload group_1
          'End
          Exit Sub
      End If
  
      If ws_vh.Range("B2") &gt; 0 Then   'Outstanding rentals?
          ui2 = MsgBox("You still have " &amp; ws_vh.Range("C2") &amp; " rentals with critical missing rental information." &amp; Chr(13) &amp; Chr(13) _
              &amp; "Active (Sports) rentals: " &amp; ws_vh.Range("B3") &amp; Chr(13) &amp; "Passive (Picnics) rentals: " &amp; ws_vh.Range("B4") &amp; Chr(13) &amp; Chr(13) _
              &amp; "Are you sure you wish to exit?", vbInformation + vbYesNo, "OUTSTANDING RENTAL INFORMATION")
          If ui2 = vbYes Then
              If ws_vh.Range("N4") &gt; 0 Then
                  Me.Label34.Caption = "    Saving unsaved rental data."
                  Me.Label34.BorderColor = RGB(50, 205, 50)
                  lastrow = ws_rd.Cells(ws_rd.Rows.Count, "A").End(xlUp).Row
                  ws_rd.Range("A3:FZ" &amp; lastrow).Sort key1:=ws_rd.Range("A3"), order1:=xlAscending, Header:=xlNo
                  Application.DisplayAlerts = False
                  ThisWorkbook.Save
                  Application.DisplayAlerts = True
                  Workbooks("Sports15c.xlsm").Activate
  
                  Debug.Print Me.Name, "EXIT1_Click() called"
                      'UserForm1.ListBox1_DeSelect ' No longer used.
                  Set oListBoxToDeselect = uf1_assess_sched.uf1_listbox3 ' [M2] This is required for the DelayedListBoxDeSelect(), if top right [X] is clicked, it won't do DeSelect
                  Unload Me
              Else
                  Unload Me
                  End
              End If
          Else
              Exit Sub
          End If
       End If
              'If ws_vh.Range("N4") &gt; 0 Then
              '    MsgBox "Unsaved rental data. Saving."
              '    lastrow = ws_rd.Cells(ws_rd.Rows.Count, "A").End(xlUp).row
              '    ws_rd.Range("A3:FZ" &amp; lastrow).Sort key1:=ws_rd.Range("A3"), order1:=xlAscending, Header:=xlNo
              '    Application.DisplayAlerts = False
              '    ThisWorkbook.Save
              '    Application.DisplayAlerts = True
              '    Unload Me
              'Else
              '    Worksheets("DYNAMIC").Activate
              '    Unload Me
              'End If
          'End If
      Unload group_1
      'Worksheets("DYNAMIC").Activate
      End
  
  End Sub
  </code></pre>
  
  <p>For the purpose of testing assume ws_vh.Range("B2") > 0</p>
  
  <p>And the independent helper module ...</p>
  
  <pre><code>Option Explicit
  
  ' Generic ListBox Deselector
  Sub ListBoxDeSelect(oListBox As Object)
      Dim i As Long
      If TypeName(oListBox) &lt;&gt; "ListBox" Then Exit Sub
      bSkipEvent = True
      With oListBox
          For i = 0 To .ListCount - 1
              If .Selected(i) Then
                  .Selected(i) = False
              End If
          Next
      End With
      bSkipEvent = False
  End Sub
  
  ' METHOD 2 [M2] - When UserForm's ShowModal = True
  Sub DelayedListBoxDeSelect()
      Dim i As Long
      If TypeName(oListBoxToDeselect) &lt;&gt; "ListBox" Then Exit Sub
      bSkipEvent = True
      With oListBoxToDeselect
          For i = 0 To .ListCount - 1
              If .Selected(i) Then
                  .Selected(i) = False
              End If
          Next
      End With
      bSkipEvent = False
      Set oListBoxToDeselect = Nothing
  End Sub
  </code></pre>
  
  <p>group_1 userform terminate code</p>
  
  <pre><code>Private Sub UserForm_Terminate()
      Debug.Print Me.Name, "UserForm_Terminate() called"
      Set oListBoxToDeselect = uf1_assess_sched.uf1_listbox3 ' [M2] This is required for the DelayedListBoxDeSelect(), if top right [X] is clicked, it won't do DeSelect
      Application.OnTime Now + TimeSerial(0, 0, 1), "DelayedListBoxDeSelect" ' [M2] Sechedules the Sub named "DelayedListBoxDeSelect" to execute in 1 second.
  End Sub
  </code></pre>
  
  <p>PART 2 - An alternate scenario requiring selection to be deselected.</p>
  
  <pre><code>If i = 0 Then
          MsgBox "Nothing to eliminate."
      '--- &gt; Deselect the user selection in uf_assess_sched.uf1_listbox2 &lt; ---
          Exit Sub
  End If
  </code></pre>