Compartir a través de


Cómo: Cambiar el formato en filas de hojas de cálculo que contienen celdas seleccionadas mediante programación

Puede cambiar la fuente de una fila completa que contiene una celda seleccionada para que el texto esté en negrita.

Se aplica a: La información de este tema se aplica a los proyectos de nivel de documento y los proyectos de nivel de aplicación para Excel 2013 y Excel 2010. Para obtener más información, vea Características disponibles por aplicación y tipo de proyecto de Office.

Para intercambiar el estilo de negrita de dos filas

  1. Declare una variable estática para realizar el seguimiento de la fila seleccionada anteriormente.

    Static previousRow As Integer = 0
    
    static int previousRow = 0;
    
  2. Recupere una referencia a la celda actual utilizando la propiedad ActiveCell.

    Dim currentCell As Excel.Range = Me.Application.ActiveCell
    
    Excel.Range currentCell = this.Application.ActiveCell;
    
  3. Aplique estilo de negrita a la fila actual utilizando la propiedad EntireRow de la celda activa.

    currentCell.EntireRow.Font.Bold = True
    
    currentCell.EntireRow.Font.Bold = true; 
    
  4. Asegúrese de que el valor actual de previousRow no es 0.El valor 0 (cero) indica que esta es la primera aparición en este código.

    If previousRow <> 0 Then
    
    if (previousRow != 0)
    
  5. Compruebe que la fila actual es distinta de la anterior.

    If currentCell.Row <> previousRow Then
    
    if (currentCell.Row != previousRow)
    
  6. Recupere una referencia a un rango que represente la fila seleccionada previamente y establezca que esa fila no esté en negrita.

    Dim rng As Excel.Range = DirectCast(ws.Rows(previousRow), Excel.Range)
    rng.EntireRow.Font.Bold = False
    
    Excel.Range rng = (Excel.Range)ws.Rows[previousRow];
    rng.EntireRow.Font.Bold = false;
    
  7. Almacene la fila actual para que pueda pasar a ser la fila anterior en la siguiente pasada.

    previousRow = currentCell.Row
    
    previousRow = currentCell.Row;
    

En el siguiente ejemplo se muestra el método completo.

Ejemplo

Private Sub BoldCurrentRow(ByVal ws As Excel.Worksheet)

    ' Keep track of the previously bolded row.
    Static previousRow As Integer = 0

    ' Work with the current active cell.
    Dim currentCell As Excel.Range = Me.Application.ActiveCell

    ' Bold the current row.
    currentCell.EntireRow.Font.Bold = True

    ' If a pass has been done previously, make the old row not bold.
    ' Make sure previousRow is not 0 (otherwise this is your first pass through).
    If previousRow <> 0 Then

        ' Make sure the current row is not the same as the previous row.
        If currentCell.Row <> previousRow Then

            Dim rng As Excel.Range = DirectCast(ws.Rows(previousRow), Excel.Range)
            rng.EntireRow.Font.Bold = False
        End If
    End If

    ' Store the new row number for the next pass.
    previousRow = currentCell.Row
End Sub
// Keep track of the previously bolded row.
static int previousRow = 0;

private void BoldCurrentRow(Excel.Worksheet ws)
{
    // Work with the current active cell.
    Excel.Range currentCell = this.Application.ActiveCell;

    // Bold the current row.
    currentCell.EntireRow.Font.Bold = true; 

    // If a pass has been done previously, make the old row not bold.
    // Make sure previousRow is not 0 (otherwise this is your first pass through).
    if (previousRow != 0)

        // Make sure the current row is not the same as the previous row.
        if (currentCell.Row != previousRow)
        {
            Excel.Range rng = (Excel.Range)ws.Rows[previousRow];
            rng.EntireRow.Font.Bold = false;
        }

    // Store the new row number for the next pass.
    previousRow = currentCell.Row;
}

Vea también

Tareas

Cómo: Aplicar estilos a rangos de libros mediante programación

Cómo: Copiar datos y formato de una hoja de cálculo al resto mediante programación

Conceptos

Trabajar con hojas de cálculo

Parámetros opcionales en las soluciones de Office