How to Select Range
IronXL provides a convenient way to select and manipulate ranges within Excel Worksheet without using Office Interop.
How to Select Range in Excel
Get started with IronXL
Start using IronXL in your project today with a free trial.
Select Range Example
With IronXL, you can perform various operations on selected ranges, such as sorting, calculations, and aggregations.
Please note
Tips
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
using IronXL;
using System.Linq;
WorkBook workBook = WorkBook.Load("sample.xls");
WorkSheet workSheet = workBook.WorkSheets.First();
// Get range from worksheet
var range = workSheet["A2:B8"];
Imports IronXL
Imports System.Linq
Private workBook As WorkBook = WorkBook.Load("sample.xls")
Private workSheet As WorkSheet = workBook.WorkSheets.First()
' Get range from worksheet
Private range = workSheet("A2:B8")
Select Row
To select the 4th row, you can use the GetRow(3)
with zero-based indexing. The range of cells will include any empty cells that are populated in other rows of the same column. This ensure that the selected row includes all the relevant cells, whether they are populated in the current row or in other rows of the same column.
:path=/static-assets/excel/content-code-examples/how-to/select-range-row.cs
using IronXL;
using System.Linq;
WorkBook workBook = WorkBook.Load("sample.xls");
WorkSheet workSheet = workBook.WorkSheets.First();
// Get row from worksheet
var row = workSheet.GetRow(3);
Imports IronXL
Imports System.Linq
Private workBook As WorkBook = WorkBook.Load("sample.xls")
Private workSheet As WorkSheet = workBook.WorkSheets.First()
' Get row from worksheet
Private row = workSheet.GetRow(3)
Select Column
To select column C, you can use the GetColumn(2)
or assign the range address as workSheet ["C:C"]
. Same with GetRow method the selected column will also include all the relevant cells, whether they are populated in the current column or in other columns of the same row.
:path=/static-assets/excel/content-code-examples/how-to/select-range-column.cs
using IronXL;
using System.Linq;
WorkBook workBook = WorkBook.Load("sample.xls");
WorkSheet workSheet = workBook.WorkSheets.First();
// Get column from worksheet
var column = workSheet.GetColumn(2);
Imports IronXL
Imports System.Linq
Private workBook As WorkBook = WorkBook.Load("sample.xls")
Private workSheet As WorkSheet = workBook.WorkSheets.First()
' Get column from worksheet
Private column = workSheet.GetColumn(2)
Tips
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, creating a new range.
Please note
Combine ranges will modify the original range. In the case of 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;
WorkBook workBook = WorkBook.Load("sample.xls");
WorkSheet workSheet = workBook.WorkSheets.First();
// Get range from worksheet
var range = workSheet["A2:B2"];
// Combine two ranges
var combinedRange = range + workSheet["A5:B5"];
Imports IronXL
Imports System.Linq
Private workBook As WorkBook = WorkBook.Load("sample.xls")
Private workSheet As WorkSheet = workBook.WorkSheets.First()
' Get range from worksheet
Private range = workSheet("A2:B2")
' Combine two ranges
Private combinedRange = range + workSheet("A5:B5")