如何突出显示表格中隐藏行中的目标列?

问题描述 投票:0回答:1

以下代码用于突出显示两个表

ListObject(1)
ListObject(2)
中的目标行和列,同时清除所有隐藏行(注意:
ListObject(1)
位于
ListObject(2)
正上方)

Sub Worksheet_SelectionChange(ByVal Target As Range)

    Dim oList1 As ListObject
    Dim oList2 As ListObject
    Dim rng As Range

    Set oList1 = Me.ListObjects(1)

    ' Contains hidden rows when filter is applied
    Set oList2 = Me.ListObjects(2)

    ' Only proceed if `Target` is inside the table body
    If Intersect(Target, oList2.DataBodyRange) Is Nothing Then Exit Sub

    Application.ScreenUpdating = False
    
    'Clear the color of all cells
    Me.Cells.Interior.ColorIndex = 0

    ' Highlights columns
    Set rng = Intersect(Target.EntireColumn, oList1.DataBodyRange)
    If Not rng Is Nothing Then
        rng.Interior.ColorIndex = 24
    End If

    ' Highlights columns
    Set rng = Intersect(Target.EntireColumn, oList2.DataBodyRange)
    rng.Interior.ColorIndex = 24

    ' Highlights rows
    Set rng = Intersect(Target.EntireRow, oList2.DataBodyRange)
    rng.Interior.ColorIndex = 38

    ' Clears all hidden rows
    For Each oRow In oList2.ListRows
        If oRow.Range.EntireRow.Hidden Then
            oRow.Range.Interior.ColorIndex = 0
        End If
        
    Next oRow

    Application.ScreenUpdating = True
End Sub

问题: 如何突出显示

ListObject(2)
上隐藏行中的目标列?

编辑1:

清除隐藏行后尝试突出显示隐藏的目标列。但我得到了

Run-time error '91': Object variable or With block variable not set

    Dim oCol As ListColumn

    ' Clear hidden row and recolor hidden target column
    For Each oRow In oList2.ListRows
        If oRow.Range.EntireRow.Hidden Then
            With Target
                oCol.Range.Interior.ColorIndex = 24
            End With
        End If
        
    Next oRow
excel vba row highlight
1个回答
0
投票

您可以将颜色格式“重新应用”到隐藏行中的列:

    ' Clears all hidden rows
    Dim oRow As ListRow
    For Each oRow In oList2.ListRows
        If oRow.Range.EntireRow.Hidden Then
            oRow.Range.Interior.ColorIndex = 0

            ' Reapply the color to the columns
            Intersect(oRow.Range, Target.EntireColumn).Interior.ColorIndex = 24
        End If
    Next
© www.soinside.com 2019 - 2024. All rights reserved.