Test if Selection is Non-continuous

Ralajer

Active Member
Joined
Jul 24, 2008
Messages
416
I have several macros that fail if the selection is not continuous. Typically they are ones that use excel's built commands or operations, I will get:
Run-time error '1004'
That command cannot be used on multiple selections.

Is there a built-in way to test if a selection is a multiple or non-continuous range, so I can switch to using a loop to instead of the faster built-in operations. I know I can write code to test the range but I have written elaborate sub's in the past only later to learn all I need was line of code.

Thanks
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
I believe you could use Areas.Count ... but someone tell me if I'm wrong as I'm too lazy to check!

I.e.

Code:
Set r = Range("X")
If r.Areas.Count = 1 Then 
    '//One area
Else
    '//More than one area
End If


I think you could even write your code to work on areas so you could still avoid cell by cell loops.
 
Upvote 0
Okay, fired up Excel after all ...
Yes this works. And you could use the same structure even if the selection is continuous (and there is only one area).

This would let you run the same code on 1 or more areas ... as long as that is feasible given what the code does.

Code:
Sub Foo()
Dim r As Range
Dim myArea As Range
Dim i As Long

Set r = Range("A1:A2,C1:C2")

For Each myArea In r.Areas
    i = i + 1
    myArea.Value = i
Next myArea

End Sub
 
Upvote 0
Thanks xenou. I had noticed the Areas property before but I never knew what it did. Now I know and it is definitely what I needed.

I will have to take the time and review the documentation on the range and other classes because there are probably quite a few properties and functions that would simplify my code a lot.

Well thanks again.

Rob
 
Upvote 0
Cheers.
If you get a copy of the Excel 2003 VBA (Programmer's Reference) by Wrox Press it has an excellent chapter on ranges. The 2002 edition is just fine too, and of course the newer 2007 edition - I happen to have the 2003 edition.
 
Upvote 0
I actually have the 2007 version but I've become so web centric I forgot I had it until you mentioned it, even though it was big, red and 4 feet away.
 
Upvote 0

Forum statistics

Threads
1,224,514
Messages
6,179,220
Members
452,895
Latest member
BILLING GUY

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