Get started with IronXL

Start using IronXL in your project today with a free trial.

First Step:
green arrow pointer


Select Range Example

With IronXL, you can perform various operations on selected ranges, such as sorting, calculations, and aggregations.

Please note
When applying methods that modify or move cell values, the affected range, row, or column will update its values accordingly.

Tips
IronXL allows us to combine more than one IronXL.Ranges.Range using the '+' operator.

Select Range

To select a range from cell A2 to B8, you can use the following code:

:path=/static-assets/excel/content-code-examples/how-to/select-range-range.cs
// Import necessary namespaces
using IronXL;
using System.Linq;

// Load the Excel workbook from the provided file path
WorkBook workBook = WorkBook.Load("sample.xls");

// Access the first worksheet within the workbook
WorkSheet workSheet = workBook.WorkSheets.First();

// Get a range from the worksheet, here capturing cells from column A to B, rows 2 to 8
Range range = workSheet["A2:B8"];

// Further operations on 'range' can be performed below, such as iterating through cells,
// reading values, checking for a specific condition, or modifying data.
' Import necessary namespaces

Imports IronXL

Imports System.Linq



' Load the Excel workbook from the provided file path

Private workBook As WorkBook = WorkBook.Load("sample.xls")



' Access the first worksheet within the workbook

Private workSheet As WorkSheet = workBook.WorkSheets.First()



' Get a range from the worksheet, here capturing cells from column A to B, rows 2 to 8

Private range As Range = workSheet("A2:B8")



' Further operations on 'range' can be performed below, such as iterating through cells,

' reading values, checking for a specific condition, or modifying data.
$vbLabelText   $csharpLabel
Select Range

Select Row

To select the 4th row, you can use the GetRow(3) method with zero-based indexing. This will include all cells in the 4th row, even if some corresponding cells in other rows are empty.

:path=/static-assets/excel/content-code-examples/how-to/select-range-row.cs
using IronXL;
using System.Linq;

// Load the Excel workbook from the specified file path
WorkBook workBook = WorkBook.Load("sample.xls");

// Select the first worksheet from the workbook
WorkSheet workSheet = workBook.WorkSheets.First();

// Get the row at index 3 (fourth row since index is zero-based) from the worksheet
var row = workSheet.Rows[3];

// Note: Add any required processing below for the 'row' variable. Depending on what you need to do with the row,
// you might enumerate through its cells, extract data, or perform some operations.

// Example: Iterate through each cell in the row and print its content.
foreach (var cell in row)
{
    System.Console.WriteLine(cell.Text);
}

// Additional processing can be done here based on requirements.
Imports IronXL

Imports System.Linq



' Load the Excel workbook from the specified file path

Private workBook As WorkBook = WorkBook.Load("sample.xls")



' Select the first worksheet from the workbook

Private workSheet As WorkSheet = workBook.WorkSheets.First()



' Get the row at index 3 (fourth row since index is zero-based) from the worksheet

Private row = workSheet.Rows(3)



' Note: Add any required processing below for the 'row' variable. Depending on what you need to do with the row,

' you might enumerate through its cells, extract data, or perform some operations.



' Example: Iterate through each cell in the row and print its content.

For Each cell In row

	System.Console.WriteLine(cell.Text)

Next cell



' Additional processing can be done here based on requirements.
$vbLabelText   $csharpLabel
Select Row

Select Column

To select column C, you can use the GetColumn(2) method or specify the range address as workSheet ["C:C"]. Like the GetRow method, it will include all relevant cells, whether they are filled in the specified column or not.

:path=/static-assets/excel/content-code-examples/how-to/select-range-column.cs
using IronXL;
using System.Linq;

// Load the workbook from the specified Excel file.
WorkBook workBook = WorkBook.Load("sample.xls");

// Get the first worksheet in the workbook.
WorkSheet workSheet = workBook.WorkSheets.First();

// Get the entire column from the worksheet.
// Note: Columns are zero-indexed in IronXL, so column 2 refers to the third column in the sheet.
var column = workSheet.Columns[2];

// Iterate through the cell values in the column
foreach (var cell in column)
{
    // Process each cell. For example, print the cell's text to the console.
    Console.WriteLine(cell.StringValue);
}
Imports IronXL

Imports System.Linq



' Load the workbook from the specified Excel file.

Private workBook As WorkBook = WorkBook.Load("sample.xls")



' Get the first worksheet in the workbook.

Private workSheet As WorkSheet = workBook.WorkSheets.First()



' Get the entire column from the worksheet.

' Note: Columns are zero-indexed in IronXL, so column 2 refers to the third column in the sheet.

Private column = workSheet.Columns(2)



' Iterate through the cell values in the column

For Each cell In column

	' Process each cell. For example, print the cell's text to the console.

	Console.WriteLine(cell.StringValue)

Next cell
$vbLabelText   $csharpLabel
Select Column

Tips
All the row and column index positions adhere to zero-based indexing.

Combine Ranges

IronXL provides the flexibility to combine multiple IronXL.Ranges.Range objects using the '+' operator. By using the '+' operator, you can easily concatenate or merge ranges together to create a new range.

Please note
Combining rows and columns directly using the '+' operator is not supported.

Combining ranges will modify the original range. In the code snippet below, the variable range will be modified to include the combined ranges.

:path=/static-assets/excel/content-code-examples/how-to/select-range-combine-range.cs
using IronXL;
using System.Linq;

// Load the workbook from the specified Excel file.
WorkBook workBook = WorkBook.Load("sample.xls");

// Select the first worksheet from the workbook.
WorkSheet workSheet = workBook.WorkSheets.First();

// Get a range of cells from the worksheet (from A2 to B2).
var range = workSheet["A2:B2"];

// To combine ranges, concatenate the Address strings and create the new range address.
// This combines the range "A2:B2" with the range "A5:B5".
string combinedAddress = $"A2:B2,A5:B5";

// Get a combined range from the specified combined addresses.
var combinedRange = workSheet[combinedAddress];

// Print out the combined range to check.
Console.WriteLine("Combined Range: " + combinedRange.ToString());
Imports IronXL

Imports System.Linq



' Load the workbook from the specified Excel file.

Private workBook As WorkBook = WorkBook.Load("sample.xls")



' Select the first worksheet from the workbook.

Private workSheet As WorkSheet = workBook.WorkSheets.First()



' Get a range of cells from the worksheet (from A2 to B2).

Private range = workSheet("A2:B2")



' To combine ranges, concatenate the Address strings and create the new range address.

' This combines the range "A2:B2" with the range "A5:B5".

Private combinedAddress As String = $"A2:B2,A5:B5"



' Get a combined range from the specified combined addresses.

Private combinedRange = workSheet(combinedAddress)



' Print out the combined range to check.

Console.WriteLine("Combined Range: " & combinedRange.ToString())
$vbLabelText   $csharpLabel

Frequently Asked Questions

How can I select a range in Excel using IronXL?

To select a range from cell A2 to B8 using IronXL, load your Excel workbook and worksheet, then use the syntax `worksheet["A2:B8"]`.

What is the method to select a row in IronXL?

You can select a row by using the `GetRow` method with zero-based indexing. For example, to select the 4th row, use `worksheet.GetRow(3)`.

How do I select a column using IronXL?

To select a column, use the `GetColumn` method with zero-based indexing. For example, to select column C, use `worksheet.GetColumn(2)` or specify the range as `worksheet["C:C"]`.

Can I combine multiple ranges in IronXL?

Yes, you can combine multiple `IronXL.Ranges.Range` objects using the '+' operator. This allows you to merge two or more ranges into a new range.

Does IronXL support zero-based indexing?

Yes, IronXL uses zero-based indexing for rows and columns.

Is Office Interop required to use IronXL for selecting ranges?

No, IronXL provides a way to select and manipulate Excel ranges without using Office Interop.

What operations can I perform on selected ranges in IronXL?

Once you've selected a range in IronXL, you can perform operations such as sorting, calculations, and aggregations.

What happens to the original range when combining ranges?

When you combine ranges using the '+' operator, the original range is modified to include the combined ranges.

Chaknith related to Combine Ranges
Software Engineer
Chaknith is the Sherlock Holmes of developers. It first occurred to him he might have a future in software engineering, when he was doing code challenges for fun. His focus is on IronXL and IronBarcode, but he takes pride in helping customers with every product. Chaknith leverages his knowledge from talking directly with customers, to help further improve the products themselves. His anecdotal feedback goes beyond Jira tickets and supports product development, documentation and marketing, to improve customer’s overall experience.When he isn’t in the office, he can be found learning about machine learning, coding and hiking.
Talk to an Expert Five Star Trust Score Rating

Ready to Get Started?

Nuget Passed