How to count rows in non-contiguous range

mcomp72

Active Member
Joined
Aug 14, 2016
Messages
305
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I need to count the number of rows in a non-contiguous range, such as: “C13, D17:E18, F22, G25, C29:F33”

If I select those cells on my worksheet using Ctrl and the left mouse button, and then run the following code:


Code:
Dim Rng As Range
 
Set Rng = Selection
 
Debug.Print Rng.Rows.Count

The result in the Immediate window is “1”. So that obviously doesn’t work with a non-contiguous range. Is there some other way it can be done?
 
Last edited by a moderator:

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Code:
Dim a As Range, c&
For Each a In Selection.Areas
    c = c + a.Rows.Count
Next
Debug.Print c

Note: this will total the rows in each area selected. If the selection has overlapping areas, it will count the overlaps separately.
If this is a problem, post again.
 
Last edited:
Upvote 0
I need to count the number of rows in a non-contiguous range,
You would also need to define exactly what you mean by "count the rows".

For example if your range is "A1:A3,C2:C4", is the row count
6 because there are 3 rows in each area, or
4 because the range actually only occupies 4 rows on the worksheet?
 
Last edited:
Upvote 0
Note: this will total the rows in each area selected. If the selection has overlapping areas, it will count the overlaps separately.
If this is a problem, post again.
If you want the rows for overlapping selection counted once only:
Code:
Dim d As Object, a As Range, i&
Set d = CreateObject("Scripting.Dictionary")
For Each a In Selection.Areas
    For i = 1 To a.Cells.Count
        d(a(i).Row) = i
    Next
Next
Debug.Print d.Count
 
Last edited:
Upvote 0
Hi Peter & footoo. Thanks for the replies. Yes, I did want rows for overlapping selections to be counted only once, so footoo, the code in your second reply looks like it will work for me. Although I will need to test it on Mac, too. The workbook I am making needs to work on both Windows & Mac, and I am not sure if CreateObject always works correctly on Mac. (There are many things that don't work the same in VBA on Mac, unfortunately.)

Another question: I see you listed a variable with a '&' after it in your code. I have not seen this before. I'm curious what the '&' signifies?
 
Last edited:
Upvote 0
Another question: I see you listed a variable with a '&' after it in your code. I have not seen this before. I'm curious what the '&' signifies?
& represents the variable type: Long

Other variable type characters:
Single !
Double #
Integer %
Currency @
String $
 
Upvote 0
Here's a possibility without using Dictionary:
Code:
Dim a As Range, c&
For Each a In Intersect(Selection.EntireRow, Columns(1))
    c = c + a.Rows.Count
Next
Debug.Print c
 
Upvote 0
Here's a possibility without using Dictionary:
Code:
Dim a As Range, c&
For Each a In Intersect(Selection.EntireRow, Columns(1))
    c = c + a.Rows.Count
Next
Debug.Print c
Comments:
I suspect that you meant ...
For Each a In Intersect(Selection.EntireRow, Columns(1)).Areas
.. for as it stands each 'a' in your code is a single cell so a.Rows.Count is 1 every time so that line would be as well to be c = c + 1

In any case, it can be done in a single line since the intersection areas in column A will never overlap
Code:
Sub CountRowsSelection()
  Debug.Print Intersect(Selection.EntireRow, Columns("A")).Cells.Count
End Sub

or if it is not actually the selection but rather a range like was mentioned in line 1 of post 1

Code:
Sub CoutRowsRange()
  Debug.Print Intersect(Range("C13, D17:E18, F22, G25, C29:F33").EntireRow, Columns("A")).Cells.Count
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,300
Members
452,633
Latest member
DougMo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top