Microsoft Excel

如果需要在 Excel 表格中自动高亮当前行,可按照以下步骤进行:

  1. 在 Excel 表格中按 Alt-F11 键,打开 Microsoft Visual Basic for Applications 窗口。
  2. 在 Project Explorer 中双击 worksheet,出现 Module 窗口,拷贝下面的 VBA 代码到该窗口。
  3. 保存并关闭 Module 窗口,返回当前 worksheet。

VBA 代码(仅高亮当前行):

Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
Static xRow
Static xColors(100)
If xRow <> Selection.Row Then
  If xRow <> "" Then
    Rows(xRow).Interior.ColorIndex = xlNone
    For i = 1 To UBound(xColors)
      Cells(xRow, i).Interior.ColorIndex = xColors(i)
    Next i
  End If
  xRow = Selection.Row
  For i = 1 To UBound(xColors)
    xColors(i) = Cells(xRow, i).Interior.ColorIndex
  Next i
  Rows(xRow).Interior.ColorIndex = 36
End If
End Sub

VBA 代码(同时高亮当前行和当前列,缺点是会清空条件格式):

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Target.Worksheet.Cells.FormatConditions.Delete
pRow = Selection.Row
pColumn = Selection.Column
With Columns(pColumn).Cells
  .FormatConditions.Add xlExpression, , "TRUE"
  .FormatConditions(1).Interior.Color = vbYellow
End With
With Rows(pRow).Cells
  .FormatConditions.Add xlExpression, , "TRUE"
  .FormatConditions(2).Interior.Color = vbYellow
End With
End Sub

WPS 表格

使用“视图 -> 阅读模式 -> 开启阅读模式”即可。

参考资料

  1. How to auto-highlight row and column of active cell in Excel?