HOW TO:在包含選取儲存格的工作表列中變更格式
您可以變更包含所選儲存格的整列字型,讓文字成為粗體字。
**適用於:**本主題中的資訊適用於 Excel 2007 和 Excel 2010 的文件層級專案和應用程式層級專案。如需詳細資訊,請參閱依 Office 應用程式和專案類型提供的功能。
若要讓目前的列成為粗體並且讓先前粗體的列成為一般字型
宣告靜態變數,以追蹤先前選取的列。
Static previousRow As Integer = 0
static int previousRow = 0;
使用 ActiveCell 屬性擷取目前儲存格的參考。
Dim currentCell As Excel.Range = Me.Application.ActiveCell
Excel.Range currentCell = this.Application.ActiveCell;
利用使用中儲存格的 EntireRow 屬性,將目前的列設為粗體樣式。
currentCell.EntireRow.Font.Bold = True
currentCell.EntireRow.Font.Bold = true;
請確認 previousRow 的目前值不是 0。 0 (零) 表示這是第一次執行這個程式碼。
If previousRow <> 0 Then
if (previousRow != 0)
確定目前的列與之前的列不同。
If currentCell.Row <> previousRow Then
if (currentCell.Row != previousRow)
擷取範圍的參考,這個範圍表示之前所選取的列,並將該列設為非粗體樣式。
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;
儲存目前的列,讓它可以成為下次通過時的上一列。
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;
}