excel 基于活动单元格的条件格式

excel conditional formatting based on active cell

我正在尝试轻松表示活动单元格与一组其他单元格之间的关系,最好使用条件格式。

我实际上是在制作甘特图,并希望能够为单元格添加不同颜色的阴影以识别依赖关系。所以我的想法是,在 A 列中我有一组任务 ID,在 B 列中我有描述,C 列中是一组依赖项。

当我选择 'B3' 之类的内容时,它会查看 C3 中的值(A 列中包含的以逗号分隔的 ID 列表)以确定 ID 所标识的匹配行在 A 列中,是依赖项 - 然后将它们着色不同。

这将根据活动单元格进行切换。

我猜这不适用于条件格式,可能需要做一些 VBA 或想出不同的方法来识别这些关系。有人做过类似的事情吗?

假设我的简单 table 设置如下:

Task ID | Description | Dependencies
--------|-------------|-------------
1       | Task One    |     
2       | Task Two    | 1
3       | Task Three  |
4       | Task Four   | 1, 3
5       | Task Five   | 1, 2, 4

当我选择“1、3”单元格(任务四旁边)时,我希望突出显示任务一和任务三。

我添加了一些 VBA 代码来实时更新一些条件格式。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Target.Calculate
    End Sub

我有一些条件格式可以突出显示与活动单元格相同行中的不同单元格:

=CELL("address")=CELL("address",$C2)

适用于我要突出显示的列 B 列

但我不想总是突出显示相邻的单元格...我想根据“依赖项”列中的值找到合适的任务。

在工作表代码中使用此事件:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim str As Variant
Dim strarr() As String
Dim j As Long
If Not Intersect(Target, Range("C:C")) Is Nothing Then
    Range("B:B").Interior.Pattern = xlNone
    If Target <> "" Then
        strarr = Split(Target, ",")
        For Each str In strarr
            j = 0
            On Error Resume Next
            j = Application.WorksheetFunction.Match(CLng(Application.Trim(str)), Range("A:A"), 0)
            On Error GoTo 0
            If j <> 0 Then
                Cells(j, 2).Interior.Color = 65535
            End If
        Next str
    End If
End If

End Sub