选择错误时突出显示单元格行

时间:2013-10-16 14:18:45

标签: excel vba

我的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

我不知道如何修复此错误。格式化很奇怪。有什么想法吗?

0 个答案:

没有答案