英文:
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
将指向所选单元格的表格,而无需硬编码表格名称。此外,如果使用 Intersect
与 Table.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
' 引用表格的数据范围(不包括标题)。
Dim trg As Range, rCount As Long
' 对于从单元格'A1'开始的表格(不是Excel(结构化)表格):
With Me.Range("A1").CurrentRegion
rCount = .Rows.Count - 1
If rCount = 0 Then Exit Sub ' 没有数据
Set trg = .Resize(rCount).Offset(1)
End With
' 对于Excel(结构化)表格:
' With Me.ListObjects("Table1")
' If .DataBodyRange Is Nothing Then Exit Sub ' 表格为空
' Set trg = .DataBodyRange
' End With
' 仅考虑表格数据范围内的单元格。
Dim irg As Range: Set irg = Intersect(trg, Target)
If irg Is Nothing Then Exit Sub
' 清除现有的高亮显示。
If CLEAR_EXISTING_HIGHLIGHTS Then
trg.Interior.ColorIndex = xlNone
End If
' 应用新的高亮显示。
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
' Reference the table's data range (exclude headers).
Dim trg As Range, rCount As Long
' For a table (not Excel (structured) table) starting in cell 'A1':
With Me.Range("A1").CurrentRegion
rCount = .Rows.Count - 1
If rCount = 0 Then Exit Sub ' no data
Set trg = .Resize(rCount).Offset(1)
End With
' For an Excel (structured) table:
' With Me.ListObjects("Table1")
' If .DataBodyRange Is Nothing Then Exit Sub ' empty table
' Set trg = .DataBodyRange
' End With
' Consider only cells in the table's data range.
Dim irg As Range: Set irg = Intersect(trg, Target)
If irg Is Nothing Then Exit Sub
' Clear existing highlights.
If CLEAR_EXISTING_HIGHLIGHTS Then
trg.Interior.ColorIndex = xlNone
End If
' 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
'ActiveSheet.UsedRange.Interior.ColorIndex = 0 'Use this if you want to clear the color from your previous selection first
With Target
Intersect(ListObjects("Table1").Range, .EntireRow).Interior.ColorIndex = 36
End With
Application.ScreenUpdating = True
End Sub
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论