# Iterating Used Cells

You can access only the cells that are used in a worksheet and iterate through them.

* [Working With The Whole Range of Used Cells](https://www.telerik.com/document-processing-libraries/documentation/libraries/radspreadprocessing/working-with-cells/iterating-used-cells#working-with-the-whole-range-of-used-cells)
* [Working With a Filtered Range](https://www.telerik.com/document-processing-libraries/documentation/libraries/radspreadprocessing/working-with-cells/iterating-used-cells#working-with-a-filtered-range)

## Working With The Whole Range of Used Cells

The [Worksheet](https://www.telerik.com/document-processing-libraries/documentation/libraries/radspreadprocessing/working-with-worksheets/what-is-worksheet) class allows you to get all the cells that are used. A cell is considered used when it has any property applied to it, no matter whether it is a value or a foreground. The `UsedCellRange` property of the `Worksheet` class returns a cell range that starts from cell `A1` and holds all cells containing data or formatting. **Example 1** shows how to get this range of cells and iterate through it.

**Example 1: Iterate through every cell inside Worksheet.UsedCellRange**

```C#
CellRange usedCellRange = worksheet.UsedCellRange;
for (int rowIndex = usedCellRange.FromIndex.RowIndex; rowIndex <= usedCellRange.ToIndex.RowIndex; rowIndex++)
{
	for (int columnIndex = usedCellRange.FromIndex.ColumnIndex; columnIndex <= usedCellRange.ToIndex.ColumnIndex; columnIndex++)
	{
		CellSelection cell = worksheet.Cells[rowIndex, columnIndex];
	}
}
```

## Working With a Filtered Range

You can get only the cells that have a particular property applied and ignore the others. Often, the property you need is the value of a cell. With the `GetUsedCellRange()` method of [Worksheet](https://www.telerik.com/document-processing-libraries/documentation/libraries/radspreadprocessing/working-with-worksheets/what-is-worksheet) you can pass an IEnumerable<[IPropertyDefinition](https://docs.telerik.com/devtools/document-processing/api/Telerik.Windows.Documents.Spreadsheet.PropertySystem.IPropertyDefinition-1.html)> object to get the used cell range holding only the cells with specific property definitions. **Example 2** shows how to get the used cell range of cells with a value and iterate through it to process each value.

**Example 2: Iterate only the used cells that contain values**

```C#
CellRange usedCellRangeWithValues = worksheet.GetUsedCellRange(new IPropertyDefinition[] { CellPropertyDefinitions.ValueProperty });
for (int row = usedCellRangeWithValues.FromIndex.RowIndex; row <= usedCellRangeWithValues.ToIndex.RowIndex; row++)
{
	for (int column = usedCellRangeWithValues.FromIndex.ColumnIndex; column <= usedCellRangeWithValues.ToIndex.ColumnIndex; column++)
	{
		ICellValue value = worksheet.Cells[row, column].GetValue().Value;
	}
}
```

> tip For more information on working with the values of the cells, check the [Get, Set and Clear Cell Properties](https://www.telerik.com/document-processing-libraries/documentation/libraries/radspreadprocessing/working-with-cells/get-set-clear-properties) and [Cell Value Types](https://www.telerik.com/document-processing-libraries/documentation/libraries/radspreadprocessing/working-with-cells/cell-value-types) topics.

The following table lists the property definitions defined by the [CellPropertyDefinitions class](https://docs.telerik.com/devtools/document-processing/api/Telerik.Windows.Documents.Spreadsheet.PropertySystem.CellPropertyDefinitions.html), which you can use to filter the used cell range:

| Property                      | Description                                                                                |
| ----------------------------- | ------------------------------------------------------------------------------------------ |
| `AllPropertyDefinitions`      | Gets all property definitions. Same behavior as `UsedCellRange` — no filtering is applied. |
| `BottomBorderProperty`        | Gets all cells that have a defined bottom border.                                          |
| `TopBorderProperty`           | Gets all cells that have a defined top border.                                             |
| `LeftBorderProperty`          | Gets all cells that have a defined left border.                                            |
| `RightBorderProperty`         | Gets all cells that have a defined right border.                                           |
| `DiagonalDownBorderProperty`  | Gets all cells that have a defined diagonal down border.                                   |
| `DiagonalUpBorderProperty`    | Gets all cells that have a defined diagonal up border.                                     |
| `DataValidationRuleProperty`  | Gets all cells that have a data validation rule applied.                                   |
| `FillProperty`                | Gets all cells that have a defined fill color.                                             |
| `FontFamilyProperty`          | Gets all cells that have a font family applied.                                            |
| `FontSizeProperty`            | Gets all cells that have a font size applied.                                              |
| `ForeColorProperty`           | Gets all cells that have a fore color applied.                                             |
| `FormatProperty`              | Gets all cells that have a specific number format defined.                                 |
| `HorizontalAlignmentProperty` | Gets all cells that have horizontal alignment applied.                                     |
| `VerticalAlignmentProperty`   | Gets all cells that have vertical alignment applied.                                       |
| `IndentProperty`              | Gets all cells that have indentation applied.                                              |
| `IsBoldProperty`              | Gets all cells that are bold.                                                              |
| `IsItalicProperty`            | Gets all cells that have italic font style applied.                                        |
| `IsLockedProperty`            | Gets all cells that are locked and will be protected when protection is applied.           |
| `IsWrappedProperty`           | Gets all cells that have text wrapping applied.                                            |
| `StyleNameProperty`           | Gets all cells that have a style applied.                                                  |
| `UnderlineProperty`           | Gets all cells that have underlining applied.                                              |
| `ValueProperty`               | Gets all cells that have a value.                                                          |

## See Also

* [What is a Worksheet?](https://www.telerik.com/document-processing-libraries/documentation/libraries/radspreadprocessing/working-with-worksheets/what-is-worksheet)
* [What is a Cell?](https://www.telerik.com/document-processing-libraries/documentation/libraries/radspreadprocessing/working-with-cells/what-is-cell)
* [Get, Set and Clear Cell Properties](https://www.telerik.com/document-processing-libraries/documentation/libraries/radspreadprocessing/working-with-cells/get-set-clear-properties)
* [Cell Value Types](https://www.telerik.com/document-processing-libraries/documentation/libraries/radspreadprocessing/working-with-cells/cell-value-types)
