Condividi tramite


Procedura: modificare la formattazione nelle righe di un foglio di lavoro contenenti celle selezionate

Aggiornamento: novembre 2007

Si applica a

Le informazioni contenute in questo argomento riguardano solo i progetti Visual Studio Tools per Office e le versioni di Microsoft Office specificati.

Tipo di progetto

  • Progetti a livello di documento

  • Progetti a livello di applicazione

Versione Microsoft Office

  • Excel 2003

  • Excel 2007

Per ulteriori informazioni, vedere Funzionalità disponibili in base ai tipi di progetto e applicazione.

È possibile modificare il tipo di carattere di un'intera riga contenente una cella selezionata per ottenere, ad esempio, testo in grassetto.

Per visualizzare in grassetto la riga corrente e in stile normale la riga precedentemente visualizzata in grassetto

  1. Dichiarare una variabile statica per tenere traccia della riga selezionata in precedenza.

    Static previousRow As Integer = 0
    
    static int previousRow = 0;
    
  2. Recuperare un riferimento alla cella corrente tramite la proprietà ActiveCell.

    Dim currentCell As Excel.Range = Me.Application.ActiveCell
    
    Excel.Range currentCell = this.Application.ActiveCell;
    
  3. Applicare lo stile in grassetto alla riga corrente tramite la proprietà EntireRow della cella attiva.

    currentCell.EntireRow.Font.Bold = True
    
    currentCell.EntireRow.Font.Bold = true; 
    
  4. Assicurarsi che il valore corrente di previousRow non sia 0. Il valore 0 (zero) indica che la riga viene utilizzata per la prima volta nel codice.

    If previousRow <> 0 Then
    
    if (previousRow != 0)
    
  5. Assicurarsi che la riga corrente sia diversa da quella precedente.

    If currentCell.Row <> previousRow Then
    
    if (currentCell.Row != previousRow)
    
  6. Recuperare un riferimento a un intervallo che rappresenta la riga selezionata in precedenza e impostare tale riga in modo che non sia in grassetto.

    Dim rng As Excel.Range = DirectCast(ws.Rows(previousRow), Excel.Range)
    rng.EntireRow.Font.Bold = False
    
    Excel.Range rng = (Excel.Range)ws.Rows[previousRow, missing];
    rng.EntireRow.Font.Bold = false;
    
  7. Archiviare la riga corrente in modo che possa diventare la riga precedente al successivo passaggio.

    previousRow = currentCell.Row
    
    previousRow = currentCell.Row;
    

Nell'esempio riportato di seguito viene illustrato il metodo completo.

Esempio

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, missing];
            rng.EntireRow.Font.Bold = false;
        }

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

Vedere anche

Attività

Procedura: applicare stili agli intervalli nei fogli di lavoro

Procedura: copiare dati e formattazione nei fogli di lavoro

Concetti

Utilizzo dei fogli di lavoro

Informazioni sui parametri facoltativi nelle soluzioni Office