只突出显示多个表格行而不是整个行。

huangapple go评论70阅读模式
英文:

Highlight multiple table row only instead of entire row

问题

只在表格范围内突出显示多个活动单元格行,而不是整个工作表行吗?
我尝试过条件格式设置,但它不适用于多个活动单元格选择。

这是我的当前工作代码。谢谢。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Application.ScreenUpdating = False

With Target
  EntireRow.Interior.ColorIndex = 36
End With

Application.ScreenUpdating = True

End Sub
英文:

Is there a way to only highlight multiple active cell rows only within the table range instead of the entire sheet row?
I have tried conditional formatting but it does not work for multiple active cell selection.

Here is my current working code. Thank you

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Application.ScreenUpdating = False

With Target
  EntireRow.Interior.ColorIndex = 36
End With

Application.ScreenUpdating = True

End Sub

答案1

得分: 3

I recommend the following:

Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim Table As ListObject
    Set Table = Target.ListObject

    If Not Table Is Nothing Then
        Dim ColorRange As Range
        Set ColorRange = Application.Intersect(Table.DataBodyRange, Target.EntireRow)
        If Not ColorRange Is Nothing Then
            ColorRange.Interior.ColorIndex = 36
        End If
    End If
End Sub

Target.ListObject 将指向所选单元格的表格,而无需硬编码表格名称。此外,如果使用 IntersectTable.DataBodyRange 进行交叉操作,它不会着色表格的标题行,而只会着色数据范围。在使用 Intersect 之前,建议始终检查这两个范围是否有交叉,否则容易出错。

请注意,使用上述代码将向表格添加带颜色的行。

如果只想着色当前选定的行,请使用以下代码:

Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim Table As ListObject
    Set Table = Target.ListObject

    If Not Table Is Nothing Then
        ' decolorize previously colored rows
        With Table.DataBodyRange.Interior
            .Pattern = xlNone
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With

        Dim ColorRange As Range
        Set ColorRange = Application.Intersect(Table.DataBodyRange, Target.EntireRow)
        If Not ColorRange Is Nothing Then
            ColorRange.Interior.ColorIndex = 36
        End If
    End If
End Sub
英文:

I recommend the following:

Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim Table As ListObject
    Set Table = Target.ListObject
    
    If Not Table Is Nothing Then
        Dim ColorRange As Range
        Set ColorRange = Application.Intersect(Table.DataBodyRange, Target.EntireRow)
        If Not ColorRange Is Nothing Then
            ColorRange.Interior.ColorIndex = 36
        End If
    End If
End Sub

Target.ListObject will point to the table of the selected cell without having to hard code the tables name. Also if you Intersect with Table.DataBodyRange it does not color the headline of the table but only the data range. Using Intersect it is always recommended to check if the two ranges intersected at all If Not ColorRange Is Nothing Then before using them, otherwise you easily run into errors.

Note that with the code above it will add colored rows to the table.
只突出显示多个表格行而不是整个行。

If you want to color only the currently selected row then use the code below:

Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim Table As ListObject
    Set Table = Target.ListObject
    
    If Not Table Is Nothing Then
        ' decolorize prevously colored rows
        With Table.DataBodyRange.Interior
            .Pattern = xlNone
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
        
        Dim ColorRange As Range
        Set ColorRange = Application.Intersect(Table.DataBodyRange, Target.EntireRow)
        If Not ColorRange Is Nothing Then
            ColorRange.Interior.ColorIndex = 36
        End If
    End If
End Sub

只突出显示多个表格行而不是整个行。

答案2

得分: 1

突出显示所选表格单元格的表格行

  • 在截图中,使用<kbd>Ctrl</kbd>+<kbd>右键单击</kbd>来选择单元格。

只突出显示多个表格行而不是整个行。

<!-- language: lang-vb -->

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Const CLEAR_EXISTING_HIGHLIGHTS As Boolean = True

    &#39; 引用表格的数据范围(不包括标题)。
    
    Dim trg As Range, rCount As Long
    
    &#39; 对于从单元格&#39;A1&#39;开始的表格(不是Excel(结构化)表格):
    With Me.Range(&quot;A1&quot;).CurrentRegion
        rCount = .Rows.Count - 1
        If rCount = 0 Then Exit Sub &#39; 没有数据
        Set trg = .Resize(rCount).Offset(1)
    End With
    
    &#39; 对于Excel(结构化)表格:
&#39;    With Me.ListObjects(&quot;Table1&quot;)
&#39;        If .DataBodyRange Is Nothing Then Exit Sub &#39; 表格为空
&#39;        Set trg = .DataBodyRange
&#39;    End With

    &#39; 仅考虑表格数据范围内的单元格。
    
    Dim irg As Range: Set irg = Intersect(trg, Target)
    If irg Is Nothing Then Exit Sub
    
    &#39; 清除现有的高亮显示。
    
    If CLEAR_EXISTING_HIGHLIGHTS Then
        trg.Interior.ColorIndex = xlNone
    End If
    
    &#39; 应用新的高亮显示。
    
    Set irg = Intersect(trg, irg.EntireRow)
    irg.Interior.ColorIndex = 36

End Sub
英文:

Highlight Table Rows of Selected Table Cell

  • In the screenshot, the cells were selected using <kbd>Ctrl</kbd>+<kbd>Right-Click</kbd>.

只突出显示多个表格行而不是整个行。

<!-- language: lang-vb -->

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Const CLEAR_EXISTING_HIGHLIGHTS As Boolean = True

    &#39; Reference the table&#39;s data range (exclude headers).
    
    Dim trg As Range, rCount As Long
    
    &#39; For a table (not Excel (structured) table) starting in cell &#39;A1&#39;:
    With Me.Range(&quot;A1&quot;).CurrentRegion
        rCount = .Rows.Count - 1
        If rCount = 0 Then Exit Sub &#39; no data
        Set trg = .Resize(rCount).Offset(1)
    End With
    
    &#39; For an Excel (structured) table:
&#39;    With Me.ListObjects(&quot;Table1&quot;)
&#39;        If .DataBodyRange Is Nothing Then Exit Sub &#39; empty table
&#39;        Set trg = .DataBodyRange
&#39;    End With

    &#39; Consider only cells in the table&#39;s data range.
    
    Dim irg As Range: Set irg = Intersect(trg, Target)
    If irg Is Nothing Then Exit Sub
    
    &#39; Clear existing highlights.
    
    If CLEAR_EXISTING_HIGHLIGHTS Then
        trg.Interior.ColorIndex = xlNone
    End If
    
    &#39; Apply new highlights.
    
    Set irg = Intersect(trg, irg.EntireRow)
    irg.Interior.ColorIndex = 36

End Sub

答案3

得分: 0

一种方法是获取所选行与表格的交集。用表格名称替换 "Table1"。

我还添加了一个建议,如果你想清除之前选择的颜色。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
    Application.ScreenUpdating = False
    
    'ActiveSheet.UsedRange.Interior.ColorIndex = 0  '如果你想先清除之前选择的颜色,请使用此行
    
    With Target
      Intersect(ListObjects("Table1").Range, .EntireRow).Interior.ColorIndex = 36
    End With
    
    Application.ScreenUpdating = True
    
End Sub
英文:

One method is to get the intersection of your selected rows and your table. Replace "Table1" with the name of your table.

I have also added a suggestion if you want to clear the color from your previous selection.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Application.ScreenUpdating = False

&#39;ActiveSheet.UsedRange.Interior.ColorIndex = 0  &#39;Use this if you want to clear the color from your previous selection first

With Target
  Intersect(ListObjects(&quot;Table1&quot;).Range, .EntireRow).Interior.ColorIndex = 36
End With

Application.ScreenUpdating = True

End Sub

huangapple
  • 本文由 发表于 2023年6月13日 12:02:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76461620.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定