Test for non contiguous range

Formula11

Active Member
Joined
Mar 1, 2005
Messages
468
Office Version
  1. 365
Platform
  1. Windows
How you check if non contiguous selection is on more than 1 row and more than 1 column.

In this case, don't continue macro.
In image below, grey cells represent selection.

Tried to test using below but don't get the expected answer.
VBA Code:
MyRange.Rows.Count
MyRange.Columns.Count

1711215748124.png
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
I suppose like me, the row count is accurate for you but not the column count? Try
Intersect(rng, rng(1, 1).EntireRow).Cells.count
Then an If block:
if row.count > 1 and column count > 1 then exit sub.

At least I think that's what you're after.
 
Upvote 0
Thanks Micron but it's not quite clear, I did something similar before but it wasn't working.
 
Upvote 0
I found something here which explains something to the effect of sub-ranges and not being sorted:

It gives the last row for example and can be added to, to include last column.

LAST ROW
VBA Code:
    Dim subRange As Range
    Dim maxRow As Long, areaMaxRow As Long

    maxRow = 0

    For Each subRange In targetRange.Areas
        areaMaxRow = subRange.Rows(subRange.Rows.Count).Row
        If areaMaxRow > maxRow Then maxRow = areaMaxRow
    Next subRange

    GetLastRowOfNonContiguousArea = maxRow


FIRST ROW
From here:
 
Last edited:
Upvote 0
Would something like this help?

VBA Code:
Sub CheckSelection()
  If Selection.Count = 1 Then
    MsgBox "Single cell"
  ElseIf Intersect(Selection, Selection.Cells(1, 1).EntireRow).Address = Selection.Address Then
    MsgBox "1 row"
  ElseIf Intersect(Selection, Selection.Cells(1, 1).EntireColumn).Address = Selection.Address Then
    MsgBox "1 column"
  Else
    MsgBox "Multiple rows/columns"
  End If
End Sub
 
Upvote 0
Maybe I'm not sure of the requirement. I figure it is, "if a non contiguous range is over more than one row and over more than one column, do something".
This lets me know how many columns:
VBA Code:
Sub testNCrange()
Dim rng As Range
Set rng = Application.InputBox(" ", " ", Type:=8)
Debug.Print rng.Address
Debug.Print Intersect(rng, rng(1, 1).EntireRow).Cells.count

End Sub
I realize it is not enough as is (it doesn't test if the range is contiguous or not) but it's how I counted the columns.
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,114
Members
453,021
Latest member
Justyna P

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