title: VBA to change slicer selection current selected item
body: <p>The below behaves quite strangely.<br>
  It's aim is to leave the slicer with only the item specified (in this case "Smith") with all other names not selected.<br>
  Most of the time it works but sometimes more than one item will be left selected.</p>
  
  <p>What is wrong with the below and how do I achieve the required behaviour?</p>
  
  <pre><code>Sub myRoutine()
      unselectAllBut "Slicer_InitialAcc_Surname", "me"  
  End Sub
  
  Public Sub unselectAllBut(slicerName As String, newSelection As String)
  
  Dim si As Object
  For Each si In ActiveWorkbook.SlicerCaches(slicerName).SlicerItems
      si.Selected = (si.Caption = newSelection)
  Next si
  
  End Sub
  </code></pre>
  
  <p>Second attempt which doesn't work either:</p>
  
  <pre><code>Public Sub unselectAllBut(slicerName As String, newSelection As String)
  
  Dim i As Integer
  With ActiveWorkbook.SlicerCaches(slicerName)
      For i = 1 To .SlicerItems.Count
          .SlicerItems(i).Selected = (.SlicerItems(i).Caption = newSelection)
      Next i
  End With
  
  End Sub
  </code></pre>
  
  <p>Maybe the data is causing the problem. It looks like the following:</p>
  
  <p><img src="https://i.stack.imgur.com/KZnSP.png" alt="enter image description here"></p>
  
  <hr>
  
  <p><strong>EDIT</strong>  </p>
  
  <p>The following seems to work. I select all items first which seems like over-kill:</p>
  
  <pre><code>Public Sub unselectAllBut(slicerName As String, newSelection As String)
  
  Dim i As Integer
  
  With ActiveWorkbook.SlicerCaches(slicerName)
      For i = 1 To .SlicerItems.Count
          .SlicerItems(i).Selected = True
      Next i
      For i = 1 To .SlicerItems.Count
          .SlicerItems(i).Selected = (.SlicerItems(i).Caption = newSelection)
      Next i
  End With
  
  End Sub
  </code></pre>