1

我的 Excel 工作表有此代码。它按您选择的行突出显示表格行,但是如果您将表格中的单元格突出显示到外部或在表格中放置切片器,则会出现问题。这是我使用的模块:

Option Explicit
Public Sub HighlightTableRow(Target As Excel.Range)

Dim t As ListObject
Dim lngInTable As Long
Dim c As Long

Const COLOR_SELECT = xlThemeColorAccent1
Const COLOR_LIGHTER = 0.4

On Error Resume Next

If Target.Interior.Pattern = xlPatternSolid Then Exit Sub

For Each t In Target.Parent.ListObjects
    c = c + 1
    If Not Intersect(Target, t.DataBodyRange) Is Nothing Then
        lngInTable = c
    End If
    t.Range.Interior.Pattern = xlNone
Next
If lngInTable = 0 Then Exit Sub

With Target.Parent.ListObjects(lngInTable)
    With .Range.Interior
        .Pattern = xlNone
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    With .DataBodyRange
        With .Resize(Target.Rows.Count).Offset(Target.Row - .Row).Interior
            .ThemeColor = COLOR_SELECT
            .TintAndShade = 1 - COLOR_LIGHTER
        End With
    End With
End With
End Sub

我把这段代码放在每个 Excel 工作表中,这样代码就可以工作了:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error Resume Next
HighlightTableRow Target
End Sub

我不知道如何解决这个错误。格式变得很奇怪。有任何想法吗?

4

0 回答 0