Como alterar a formatação em linhas de planilhas que contêm células selecionadas programaticamente

Você pode alterar a fonte de uma linha inteira que contém uma célula selecionada para que o texto seja negrito.

Aplicável a: As informações neste tópico se aplicam a projetos de nível de documento e projetos de nível de aplicativo para Excel 2013 e Excel 2010. Para obter mais informações, consulte Recursos disponíveis pelo aplicativo do Office e o tipo de projeto.

Para fazer a linha atual em negrito a linha anteriormente negrito e normal

  1. Declare uma variável estática para manter controle da linha selecionada anteriormente.

    Static previousRow As Integer = 0
    
    static int previousRow = 0;
    
  2. Recuperar uma referência para a célula atual usando a propriedade de ActiveCell .

    Dim currentCell As Excel.Range = Me.Application.ActiveCell
    
    Excel.Range currentCell = this.Application.ActiveCell;
    
  3. Estilize a linha atual em negrito usando a propriedade de EntireRow da célula ativo.

    currentCell.EntireRow.Font.Bold = True
    
    currentCell.EntireRow.Font.Bold = true; 
    
  4. Certifique-se de que o valor atual de previousRow não é 0.Um 0 (zero) indica que este é a primeira vez com este código.

    If previousRow <> 0 Then
    
    if (previousRow != 0)
    
  5. Certifique-se de que a linha atual seja diferente da linha anterior.

    If currentCell.Row <> previousRow Then
    
    if (currentCell.Row != previousRow)
    
  6. Recuperar uma referência a um intervalo que representa a linha que foi selecionada anteriormente, defina essa linha não seja negrito.

    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. Armazenar a linha atual para que possa assentar bem na linha anterior no próximo passo.

    previousRow = currentCell.Row
    
    previousRow = currentCell.Row;
    

O exemplo a seguir mostra o método completo.

Exemplo

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;
}

Consulte também

Tarefas

Como aplicar estilos a intervalos em pastas de trabalho programaticamente

Como copiar dados e formatar em planilhas programaticamente

Conceitos

Trabalhando com planilhas

Parâmetros opcionais em soluções do Office