Returns a new range which consists of the intersection of this range and
range2, or
null if the ranges do not intersect.
Syntax
Parameters
- range2
- Specifies the range to intersect with this range.
Return Value
A new range consisting of the intersection of this range and
range2, or
null if the ranges do not intersect.
Example
C# | Copy Code |
---|
public void RangeIntersectUnionSubtract()
{
// Create a workbook to play with.
IWorkbook workbook = Factory.GetWorkbook();
IWorksheet worksheet = workbook.Worksheets[0];
IRange cells = worksheet.Cells;
// Create some ranges.
IRange a1 = cells["A1"];
IRange a1_b3 = cells["A1:B3"];
IRange b3_d4 = cells["B3:D4"];
// A variable to hold results.
IRange range;
// A1 does not intersect with B3:D4
range = a1.Intersect(b3_d4);
Debug.Assert(range == null);
// Subtracting B3:D4 from A1 does not change A1.
range = a1.Subtract(b3_d4);
Debug.Assert(range != null && range.Address.Equals("$A$1"));
// The union of A1 and B3:D4 is "A1,B3:D4".
range = a1.Union(b3_d4);
Debug.Assert(range != null && range.Address.Equals("$A$1,$B$3:$D$4"));
// B3:D4 does not intersect with A1.
range = b3_d4.Intersect(a1);
Debug.Assert(range == null);
// Subtracting A1 from B3:D4 does not change B3:D4.
range = b3_d4.Subtract(a1);
Debug.Assert(range != null && range.Address.Equals("$B$3:$D$4"));
// The union of B3:D4 and A1 is "A1,B3:D4".
range = b3_d4.Union(a1);
Debug.Assert(range != null && range.Address.Equals("$A$1,$B$3:$D$4"));
// The intersection of B3:D4 and A1:B3 is B3
range = b3_d4.Intersect(a1_b3);
Debug.Assert(range != null && range.Address.Equals("$B$3"));
// Subtracting A1:B3 from B3:D4 leaves "C3:D3,B4:D4".
range = b3_d4.Subtract(a1_b3);
Debug.Assert(range != null && range.Address.Equals("$C$3:$D$3,$B$4:$D$4"));
// The union of B3:D4 and A1:B3 is "A1:B2,A3:D3,B4:D4".
range = b3_d4.Union(a1_b3);
Debug.Assert(range != null && range.Address.Equals("$A$1:$B$2,$A$3:$D$3,$B$4:$D$4"));
// The intersection of "A1,B1,A2,B2" and "A1:B1,A2:B2" is A1:B2.
range = cells["A1,B1,A2,B2"].Intersect(cells["A1:B1,A2:B2"]);
Debug.Assert(range != null && range.Address.Equals("$A$1:$B$2"));
} |
Visual Basic | Copy Code |
---|
Public Sub RangeIntersectUnionSubtract()
' Create a workbook to play with.
Dim workbook As IWorkbook = Factory.GetWorkbook()
Dim worksheet As IWorksheet = workbook.Worksheets(0)
Dim cells As IRange = worksheet.Cells
' Create some ranges.
Dim a1 As IRange = cells("A1")
Dim a1_b3 As IRange = cells("A1:B3")
Dim b3_d4 As IRange = cells("B3:D4")
' A variable to hold results.
Dim range As IRange
' A1 does not intersect with B3:D4
range = a1.Intersect(b3_d4)
Debug.Assert(range Is Nothing)
' Subtracting B3:D4 from A1 does not change A1.
range = a1.Subtract(b3_d4)
Debug.Assert(Not (range Is Nothing) And range.Address.Equals("$A$1"))
' The union of A1 and B3:D4 is "A1,B3:D4".
range = a1.Union(b3_d4)
Debug.Assert(Not (range Is Nothing) And range.Address.Equals("$A$1,$B$3:$D$4"))
' B3:D4 does not intersect with A1.
range = b3_d4.Intersect(a1)
Debug.Assert(range Is Nothing)
' Subtracting A1 from B3:D4 does not change B3:D4.
range = b3_d4.Subtract(a1)
Debug.Assert(Not (range Is Nothing) And range.Address.Equals("$B$3:$D$4"))
' The union of B3:D4 and A1 is "A1,B3:D4".
range = b3_d4.Union(a1)
Debug.Assert(Not (range Is Nothing) And range.Address.Equals("$A$1,$B$3:$D$4"))
' The intersection of B3:D4 and A1:B3 is B3
range = b3_d4.Intersect(a1_b3)
Debug.Assert(Not (range Is Nothing) And range.Address.Equals("$B$3"))
' Subtracting A1:B3 from B3:D4 leaves "C3:D3,B4:D4".
range = b3_d4.Subtract(a1_b3)
Debug.Assert(Not (range Is Nothing) And range.Address.Equals("$C$3:$D$3,$B$4:$D$4"))
' The union of B3:D4 and A1:B3 is "A1:B2,A3:D3,B4:D4".
range = b3_d4.Union(a1_b3)
Debug.Assert(Not (range Is Nothing) And range.Address.Equals("$A$1:$B$2,$A$3:$D$3,$B$4:$D$4"))
' The intersection of "A1,B1,A2,B2" and "A1:B1,A2:B2" is A1:B2.
range = cells("A1,B1,A2,B2").Intersect(cells("A1:B1,A2:B2"))
Debug.Assert(Not (range Is Nothing) And range.Address.Equals("$A$1:$B$2"))
End Sub |
Requirements
Target Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Windows Vista, Windows Server 2008 family
See Also