If someone stumbles upon this thread like I did today...

The code works if a row or cell is clicked but fails if you click on the checkbox in the header which still selects all rows.

To fix it...