How to: Programmatically Change Formatting in Worksheet Rows Containing Selected Cells

 

You can change the font of an entire row that contains a selected cell so that the text is bold.

Applies to: The information in this topic applies to document-level projects and VSTO add-in projects for Excel. For more information, see Features Available by Office Application and Project Type.

To make the current row bold and the previously bolded row normal

  1. Declare a static variable to keep track of the previously selected row.

    No code example is currently available or this language may not be supported.

  2. Retrieve a reference to the current cell using the ActiveCell property.

    No code example is currently available or this language may not be supported.

  3. Style the current row bold using the EntireRow property of the active cell.

    No code example is currently available or this language may not be supported.

  4. Ensure that the current value of previousRow is not 0. A 0 (zero) indicates that this is the first time through this code.

    No code example is currently available or this language may not be supported.

  5. Ensure that the current row is different from the previous row.

    No code example is currently available or this language may not be supported.

  6. Retrieve a reference to a range that represents the row that was previously selected, and set that row to not be bold.

    No code example is currently available or this language may not be supported.

  7. Store the current row so that it can become the previous row on the next pass.

    No code example is currently available or this language may not be supported.

The following example shows the complete method.

No code example is currently available or this language may not be supported.

Working with Worksheets
How to: Programmatically Apply Styles to Ranges in Workbooks
How to: Programmatically Copy Data and Formatting across Worksheets
Optional Parameters in Office Solutions

Show: