Articles in this section
Category / Section

How to get the intersection of two ranges in Excel worksheet using C#, VB.NET

2 mins read

Syncfusion Essential XlsIO is a .NET Excel library used to create, read, and edit Excel documents. Also, converts Excel documents to PDF files. Using this library, you can get the intersection of any two Excel ranges in C#, VB.NET.

Steps to get the intersection of two ranges in Excel, programmatically:

1. Create a new C# console application project.

Create new console application project

 Create new console application project

2. Install the Syncfusion.XlsIO.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.

Install XlsIO NuGet package

 Install XlsIO NuGet package

3. Include the following namespace in the Program.cs file.

C#

using Syncfusion.XlsIO;

 

VB.NET

Imports Syncfusion.XlsIO

 

4. Use the following code snippet to get the intersection of two ranges in Excel worksheet using C#, VB.NET.

C#

using (ExcelEngine excelEngine = new ExcelEngine())
{
    //Instantiate the application object
    IApplication application = excelEngine.Excel;
 
    //Open the template workbook with dummy data
    IWorkbook workbook = application.Workbooks.Open("TemplateData.xlsx", ExcelOpenType.Automatic);
 
    //Accessing the first worksheet
    IWorksheet worksheet = workbook.Worksheets[0];
 
    //Check if the ranges "C1:G10" and "D3:Q6" intersect
    IRange intersectRange = worksheet.IntersectRanges(worksheet.Range["C1:G10"], worksheet.Range["D3:Q6"]);
    if( intersectRange != null)
    {
        Console.WriteLine("true");
        Console.WriteLine("Intersecting range: " + intersectRange.AddressLocal);
        Console.ReadKey();
    }
    else
    {
        Console.WriteLine("false");
        Console.ReadKey();
    }
}

 

VB.NET

Using excelEngine As ExcelEngine = New ExcelEngine()
    'Instantiate the application object
    Dim application As IApplication = excelEngine.Excel
 
    'Open the template workbook with dummy data
    Dim workbook As IWorkbook = application.Workbooks.Open("TemplateData.xlsx", ExcelOpenType.Automatic)
 
    'Accessing the first worksheet
    Dim worksheet As IWorksheet = workbook.Worksheets(0)
 
    'Check if the ranges "C1:G10" and "D3:Q6" intersect
    Dim intersectRange As IRange = worksheet.IntersectRanges(worksheet.Range("C1:G10"), worksheet.Range("D3:Q6"))
    If intersectRange IsNot Nothing Then
        Console.WriteLine("true")
        Console.WriteLine("Intersecting range: " + intersectRange.AddressLocal)
        Console.ReadKey()
    Else
        Console.WriteLine("false")
        Console.ReadKey()
    End If
End Using

 

A complete working example to find the intersection of two ranges in Excel can be downloaded from Intersection of two ranges in Excel.zip.

Take a moment to peruse the documentation, where you can find basic worksheet data manipulation options along with features like Conditional Formatting, worksheet calculations through Formulas, adding Charts in worksheet or workbook, organizing and analyzing data through Tables and Pivot Tables, appending multiple records to worksheet using Template Markers, and most importantly PDF and Image conversions etc. with code examples.

Click here to explore the rich set of Syncfusion Excel (XlsIO) library features.

See Also:

How to update formula for each row using Template Markers

Merge Excel cells in C#, VB.NET

Paste only the formula value of Excel cell in C#, VB.NET

How to set column width and row height?

How can we avoid the ‘Unexpected token’ exception while using formula in XlsIO?

How to add text box shape in Excel charts?

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer the link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.

 

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied