excel 突出显示整行,而不仅仅是单元格

siotufzp  于 2023-11-20  发布在  其他
关注(0)|答案(1)|浏览(147)

我想让HashCode从一个单元格中提取一种颜色,该单元格具有由条件格式设置的颜色,并将其应用于整行。我有下面的代码,但是,它只将颜色应用于特定的列。我如何更改代码以突出显示整行而不仅仅是列?

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim xSRg, xDRg, xPRG, xISRg, xIDRg As Range
Dim xFNum As Long
On Error Resume Next
Set xSRg = Range("A2:A84")
Set xDRg = Range("B2:B84")
For xFNum = 1 To xSRg.Count
Set xISRg = xSRg.Item(xFNum)
Set xIDRg = xDRg.Item(xFNum)
xIDRg.Interior.Color = xISRg.DisplayFormat.Interior.Color
Next xFNum
End Sub

字符串
目前它的工作原理如下所示,但我想颜色适用于从A13到J13。
enter image description here

aiazj4mn

aiazj4mn1#

如果它涉及到对用户单击的任何单元格的整行执行某些操作,则可以简单地用途:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  Rows(Target.Row).Select
End Sub

字符串
例如,要将背景色应用于整行:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  Rows(Target.Row).Interior.Color = vbYellow
End Sub


在这些示例中,如果用户选择了多个单元格,则将使用所选区域的“顶”行。
若要将动作套用至所有选取的列,您可以在这些列之间进行循环:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  Dim rw
  For Each rw In Target.Rows
    Rows(rw.Row).Interior.Color = vbRed
  Next rw
End Sub


......或者,从选定单元格的任意区域中选择所有行将是一个类似的循环,使用Union将每一行添加到一个区域

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  Dim rw, sel As Range
  For Each rw In Target.Rows  'loop through selected rows
    If sel Is Nothing Then
      Set sel = Rows(rw.Row) 'if this the 1st selected row, store it
    Else
      Set sel = Application.Union(sel, Rows(rw.Row)) 'merge with prev
    End If
  Next rw
  sel.Select 'select range of rows
End Sub

相关问题