A Range object specifies a cell or a group of cells on a worksheet. Once you have created a Range object, you can format a cell or group of cells.
You can use the Range property from the Worksheet object to specify a range in a worksheet.
{Specify a range that includes cells A1 to A5.} cellRange = worksheet.Range["A1:A5"];
You can also use the Columns property to specify a range. The Columns property can be used with three different objects: the Application object, the Worksheet object, and the Range object. The Columns property from the Application object returns a range containing all of the columns in the active worksheet.
{Return a Range object containing the active worksheet’s columns.} cellRange = app.Columns;
The Columns property from the Worksheet object returns a range containing all of the columns on the worksheet.
{Return a Range object containing all of the columns in the worksheet.} cellRange = worksheet.Columns;
You can also use the Rows property to specify a range. The Rows property can also be used with three different objects: the Application object, the Worksheet object, and the Range object. The Rows property from the Application object returns a range containing all of the rows in the active worksheet.
{Return a Range object containing the active worksheet’s rows.} cellRange = app.Rows;
The Rows property from the Worksheet object returns a range containing all of the rows on the worksheet.
{Return a Range object containing all of the rows in the worksheet.} cellRange = worksheet.Rows;
Use the Value property to insert text into a specified range. If the range contains multiple cells, the Value property inserts the text into the active cell.
{Insert text into a cell.} cellRange.Value = "Customer ID";
You can also use the Value property to retrieve the unformatted value of a specified range. If the range contains multiple cells, the Value property returns the value from the active cell.
{Retrieve the unformatted value of the cell.} intCellValue = integer(cellRange.Value);
To retrieve the formatted value of a cell as a string, use the Text property.
{Retrieve the formatted value of a cell as a string.} stringCellValue = string(cellRange.Text);
The Range object contains several properties to modify the appearance of a worksheet, such as the Font property, the Borders property, the Columns property, and the Interior property.
Font property The Font property returns a Font object. The Font object contains properties that modify characteristics of the font, such as style and size.
{Set the font style.} cellRange.Font.Bold = true; {Set the font size.} cellRange.Font.Size = 8;
Borders property The Borders property returns a Borders collection that contains all of the cell borders. You can use the Borders collection to set properties for borders, such as the weight and line style.
{Use a constant from the xlBorderWeight enumeration to set the weight of the borders.} cellRange.Borders.Weight = Excel.xlHairline; {Use a constant from the xlLineStyle enumeration to set the line style of the borders.} cellRange.Borders.LineStyle = Excel.xlDot;
You can also access individual borders in a range with the Borders collection. To specify a border, use the Item property and the constants contained in the xlBordersIndex enumeration.
{Use a constant from the xlLineStyle enumeration to set the line style for the bottom border.} cellRange.Borders.Item[Excel.xlEdgeBottom].LineStyle = Excel.xlDot;
Interior property The Interior property returns an Interior object. The Interior object contains properties that modify the characteristics of the interior of a cell, such as the cell’s background color and background pattern.
{Set the background color.} cellRange.Interior.ColorIndex = 35; {Use a constant from the xlPattern enumeration to set the background pattern.} cellRange.Interior.Pattern = Excel.xlPatternDown;