方法 : 選択されたセルを含むワークシートの行で書式を変更する

更新 : 2007 年 11 月

対象

このトピックの情報は、指定された Visual Studio Tools for Office プロジェクトおよび Microsoft Office のバージョンにのみ適用されます。

プロジェクトの種類

  • ドキュメント レベルのプロジェクト

  • アプリケーション レベルのプロジェクト

Microsoft Office のバージョン

  • Excel 2003

  • Excel 2007

詳細については、「アプリケーションおよびプロジェクトの種類別の使用可能な機能」を参照してください。

選択されているセルを含む行全体のフォントを変更してテキストを太字にすることができます。

現在の行を太字に設定し、太字に設定されていた行を標準に戻すには

  1. 直前に選択された行を追跡するための静的変数を宣言します。

    Static previousRow As Integer = 0
    
    static int previousRow = 0;
    
  2. ActiveCell プロパティを使用して、現在のセルへの参照を取得します。

    Dim currentCell As Excel.Range = Me.Application.ActiveCell
    
    Excel.Range currentCell = this.Application.ActiveCell;
    
  3. アクティブなセルの EntireRow プロパティを使用して、現在の行を太字にします。

    currentCell.EntireRow.Font.Bold = True
    
    currentCell.EntireRow.Font.Bold = true; 
    
  4. previousRow の現在の値が 0 でないことを確認します。0 (ゼロ) は、このコードの 1 回目の実行であることを意味します。

    If previousRow <> 0 Then
    
    if (previousRow != 0)
    
  5. 現在の行が直前の行と異なることを確認します。

    If currentCell.Row <> previousRow Then
    
    if (currentCell.Row != previousRow)
    
  6. 直前に選択されていた行を表す範囲の参照を取得し、その行の太字設定を解除します。

    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. 現在の行を格納して、次にコードを実行したときに現在の行が直前の行になるようにします。

    previousRow = currentCell.Row
    
    previousRow = currentCell.Row;
    

このメソッドの完全なコードは次のようになります。

使用例

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

参照

処理手順

方法 : ブック内の範囲にスタイルを適用する

方法 : 複数のワークシートにデータと書式設定をコピーする

概念

ワークシートの操作

Office ソリューションの省略可能なパラメータについて