title: VBA Rows.Count in Selection
body: <p>I'm looking to work out how many rows a user has selected to be displayed at the top of the sheet next to an action button, I.e. Button says "Generate Email" and next to it says "x items selected". </p>
  
  <p>As this is updated everytime the selection is changed, I have the following code: </p>
  
  <pre><code>Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Sheet1.Range("E1") = Target.Rows.Count &amp; " items selected"
  End Sub
  </code></pre>
  
  <p>This works fine if the user selects consecutive rows, for e.g. 7:10 returns 4. </p>
  
  <p>My problem is if a user selected rows 7, and 10. It would only return 1 (the rows in the first part of the selection). </p>
  
  <p>From what I've found, there is no way of just getting this value from a property, but I can't get my head around how to iterate through all parts of the selection/target and calculate the sum of rows. Then there is also the possibility that the user selects say A7, C7, and A10. A7 and C7 relate to the same item, so this should only really be treated as one, not two, which I think my hypothetical code would do...</p>
  
  <p>Has anyone tried to achieve this before and been successful or could point me in the direction of some properties which may help? I tried a separate function to achieve it, but that wasn't working either.</p>
  
  <pre><code>Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Sheet1.Range("E1") = getRowCount(Target) &amp; " items selected"
  End Sub
  
  Function getRowCount(selectedRanges As Ranges)
    rowCount = 0
    For Each subRange In selectedRanges
      rowCount = rowCount + subRange.Rows.Count
    Next
  
    getRowCount = rowCount
  End Function
  </code></pre>