Worksheet.SetSelectedRanges(IList<CellRange>, Boolean) Method
Selects cell ranges in the worksheet and specifies whether the selection expands to fit merged cells.
A list of the CellRange objects.
true, to expand the selection to include merged cells; otherwise, false.
true, if cell ranges are selected successfully; otherwise false.
To select multiple non-adjacent cells or cell ranges in the worksheet simultaneously, use the SetSelectedRanges method. The expandToMergedCellsSize parameter enables you to determine whether to create a selection including merged cells, as illustrated below.
Worksheet sheet = spreadsheetControl1.ActiveWorksheet; sheet.MergeCells(sheet.Range["E5:F9"]); List<CellRange> mylist = new List<CellRange>(); mylist.Add(sheet.Range["A2:C4"]); mylist.Add(sheet.Range["C6:C9"]); mylist.Add(sheet.Range["E3:E7"]); sheet.SetSelectedRanges(mylist, true);
The code results in a selection that includes merged cells, as shown in the following picture.
If the expandToMergedCellsSize parameter in the SetSelectedRanges method is set to false, the selection does not expand, as shown below:
When you call the SetSelectedRanges method, an active cell is automatically set to the top left cell of the first range in the passed list.
The Worksheet.Selection property specifies the range of cells selected in the worksheet. The Worksheet.SelectedCell property specifies a single cell where data is inserted when an end-user types (in other words, this is the active cell). The Selection can be a single cell or contiguous or noncontiguous (union) range of cells, while the SelectedCell is always a single cell inside the current selection. If the cell you assign to SelectedCell is outside the currently selected range, the selection will be changed - it will coincide with the specified active cell.