We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Used range includes empty cells. How do I avoid this?

Platform: WinForms |
Control: XlsIO |
Published Date: April 30, 2011 |
Last Revised Date: April 11, 2019

Microsoft Excel allows you to select all the cells in the used range using the following shortcut sequence:

  1. Press Ctrl + Home, to select cell A1.
  2. Press Ctrl + Shift + End, to select all cells from A1 to the last used cell.

Micrsoft Excel output

As you can see in the screen shot above, there is nothing visible in cells of 15th and 16th rows, but it is included in the selected used range. Perhaps there was a value in that cell, and it was deleted, or the cell is formatted. Let us see how to avoid the empty cells and select the actual used range in an Excel document.

Syncfusion Excel (XlsIO) library 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 select actual used range in an Excel document excluding the empty cells that contain formatting without text.

Steps to select used range in a sample document without including the empty cells, programmatically:

Step 1: Create a new C# console application project.

Create a new C# console application

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

Install NuGet package

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

C#

using Syncfusion.XlsIO;

 

VB.NET

Imports Syncfusion.XlsIO

 

Step 4: Use the following code snippet to select the used range without empty cells in the sample Excel document, and to add border around the selected cells which highlights the actual used range in the saved Excel document.

C#

using (ExcelEngine excelEngine = new ExcelEngine())
{
    //Instantiate the application object
    IApplication application = excelEngine.Excel;
 
    //Add a workbook
    IWorkbook workbook = application.Workbooks.Open("Sample.xlsx");
 
    //Get the first sheet
    IWorksheet sheet = workbook.Worksheets[0];
 
    //UsedRange excludes the blank cells
    sheet.UsedRangeIncludesFormatting = false;
 
    //Adding border to highlight the used range
    sheet.UsedRange.BorderAround();
 
    //Save the file
    workbook.SaveAs("UsedRange.xlsx");
 
    System.Diagnostics.Process.Start("UsedRange.xlsx");
}

 

VB.NET

Using excelEngine As ExcelEngine = New ExcelEngine()
    'Instantiate the application object
    Dim application As IApplication = excelEngine.Excel
 
    'Add a workbook
    Dim workbook As IWorkbook = application.Workbooks.Open("Sample.xlsx")
 
    'Get the first sheet
    Dim sheet As IWorksheet = workbook.Worksheets(0)
 
    'UsedRange excludes the blank cells
    sheet.UsedRangeIncludesFormatting = False
 
    'Adding border to highlight the used range
    sheet.UsedRange.BorderAround()
 
    'Save the file
    workbook.SaveAs("UsedRange.xlsx")
 
    System.Diagnostics.Process.Start("UsedRange.xlsx")
End Using

 

A complete Windows Forms working example of how to exclude empty cells used range in an Excel document can be downloaded from exclude empty cells in Excel used range.zip.

By executing the program, you will get the output Excel file as shown below.

Output Excel document

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

See Also:

How to set an active cell in a worksheet?

How to retrieve cell value without enabling sheet calculations in C#,VB.NET?

Is it possible to AutoFit a row that contains a cell merged with cells in other rows?

How to resize comment box using C#,VB.NET?

How to set Freeze panes in Excel using C#,VB.NET?

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.

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment
Comments
seetharaman
Mar 09, 2018

sometimes when i give this , it gives compile error , some times run time error 424!!!

Reply
Abirami Varadharajan [Syncfusion]
Mar 20, 2018

There should not be any compilation error or runtime exception while using this property.

 
API Reference: https://help.syncfusion.com/cr/cref_files/file-formats/xlsio/Syncfusion.XlsIO.Base~Syncfusion.XlsIO.IWorksheet~UsedRangeIncludesFormatting.html
 
This property works fine and we recommend you to upgrade your Syncfusion version to latest one if you are using older version.
 
In-case if there is any error or exception, please share us the error/exception stack trace to check further.

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon