如果活动单元格(两行或更多行)在同一列中,则突出显示单元格

时间:2019-02-18 01:58:14

标签: excel excel-formula

我知道如何使用条件格式突出显示单个活动行的单元格

=CELL("address")=CELL("address",C$5)

如何执行但有两行或更多行(5、7、9、11)

2 个答案:

答案 0 :(得分:0)

您的条件格式基于三个条件的AND。

=and(row()>=5, row()<=11, mod(row(), 2)=1)

Excel的最新版本可以使用isodd(row())代替mod(row(), 2)=1

答案 1 :(得分:0)

指定行中的突出显示单元格

  • 在Visual Basic编辑器( Alt + F11 )中,将此代码复制到工作表中 您要在其中运行此工作表的窗口(双击) 代码。
  • 在常量部分调整值(cRngcRangecRow cColor) 满足您的需求。

代码

Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Const cRng As String = "C3:AM3"     ' Target Range
    Const cRange As String = "C5:AM27"  ' Source Range
    Const cRow As Long = 3              ' Target Row Number
    Const cColor As Long = 3            ' Color Index e.g. 3 is Red.

    Dim rng As Range  ' Intersect Range
    Dim i As Long     ' Rows Counter
    Dim k As Long     ' Areas Counter

    ' Create a reference to Intersect Range.
    Set rng = Intersect(Target, Range(cRange))

    ' Remove color in all cells of Target Range.
    Range(cRng).Interior.ColorIndex = xlNone

    If Not rng Is Nothing Then
        ' Loop through Areas of Intersect Range.
        For k = 1 To rng.Areas.Count
            ' Loop through rows of current Area of Intersect Range.
            For i = 1 To rng.Areas(k).Rows.Count
                ' In current Area of Intersect Range
                With rng.Areas(k)
                    ' Check if current row number of current area of Intersect
                    ' range is odd.
                    If .Rows(i).Row Mod 2 = 1 Then
                        ' Apply color to all cells in row cRow of Worksheet
                        ' whose columns are the same as those of Current Area
                        ' of Intersect Range.
                        Cells(cRow, .Column).Resize(, .Columns.Count) _
                            .Interior.ColorIndex = cColor
                        Exit For
                    End If
                End With
            Next ' Row of current Area of Intersect Range.
        Next ' Area of Intersect Range.
    End If

End Sub