如何:在包含选定单元格的工作表行中更改格式设置
可以更改包含选定单元格的整行的字体,使其中的文本变成粗体。
**适用于:**本主题中的信息适用于 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;
}