View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Tushar Mehta Tushar Mehta is offline
external usenet poster
 
Posts: 1,071
Default Exception when uisng Intersect method

A test that should be safer than checking the name of the worksheet is
to use the Is comparison operator as in:

Option Explicit

Sub testIt()
Dim Rng1 As Range, Rng2 As Range
Set Rng1 = Sheets(1).Range("a1")
Set Rng2 = Sheets(1).Range("a2")
MsgBox Rng1.Parent Is Rng2.Parent
Set Rng1 = Sheets(1).Range("a1")
Set Rng2 = Sheets(2).Range("a1")
MsgBox Rng1.Parent Is Rng2.Parent
Set Rng1 = Workbooks(1).Sheets(1).Range("a1")
Set Rng2 = Workbooks(2).Sheets(1).Range("a1")
MsgBox Rng1.Parent Is Rng2.Parent
Set Rng1 = Workbooks(1).Sheets(1).Range("a1")
Set Rng2 = Workbooks(1).Sheets(1).Range("a2")
MsgBox Rng1.Parent Is Rng2.Parent
End Sub

The above code should yield: True, False, False, True.

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

In article ,
says...
I just wanted to have a more generic method to basically check if a range is overlapping with any other range in the same workbook. Is there a simple method to check if two ranges are in different sheets?

Thanks