Counting Highlighted Rows

  • Thread starter Thread starter Phil H
  • Start date Start date
P

Phil H

I am highlighting rows beyond what I see on screen. Can someone give me a
macro that counts the number of highlighted rows.
 
By "highlighting", do you mean "selecting"? If so, then give this macro a
try (it will handle both contiguous and non contiguous selections)...

Sub CountRowsInSelection()
Dim A As Range, U As Range
For Each A In Selection.Areas
If U Is Nothing Then
Set U = A.EntireRow
Else
Set U = Union(U, A.EntireRow)
End If
Next
MsgBox Intersect(U, Columns(1)).Count
End Sub
 
Your code will not work if the Selection contains non contiguous areas with
non overlapping rows (for example, C3:F9 and H14:M20).
 
Works great. Thanks, Luke.

Luke M said:
Sub HowMany()
MsgBox Selection.Rows.Count
End Sub
--
Best Regards,

Luke M
*Remember to click "yes" if this post helped you!*
 
I'm guessing, then, that you will never have selections composed of
non-contiguous areas. For future consideration, you should really mention
such things in your questions when you ask them... the more information you
give about your set up and what you want to do, the better able we are to
give you solutions that do what you are looking for.
 
If highlighting rows means selecting entire rows, I think this would be
enough.

MsgBox Intersect(Selection, Columns(1)).Count

If not entire rows, then

MsgBox Intersect(Selection.EntireRow, Columns(1)).Count

Keiji
 
Back
Top