Counting Highlighted Rows

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.
 
R

Rick Rothstein

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
 
R

Rick Rothstein

Your code will not work if the Selection contains non contiguous areas with
non overlapping rows (for example, C3:F9 and H14:M20).
 
P

Phil H

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!*
 
R

Rick Rothstein

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.
 
K

keiji kounoike

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
 
R

Rick Rothstein

If not entire rows, then
MsgBox Intersect(Selection.EntireRow, Columns(1)).Count

Yes, that works fine and is **much** better than the code I posted!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top