excel单元格所在行与列高亮显示

2024年11月28日 09:46
有2个网友回答
网友(1):

执行 开关高亮() 以打开或关闭高亮显示光标所在的行和列
如果不设置这个开关,单元格背景色缓存中保存的颜色会刷新你刚刚修改的单元格颜色,而引起你修改颜色失败.
代码如下
Dim arrH(), arrL()
Dim H1, L1, irange
Dim yn As Boolean
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If yn = False Then Exit Sub
Dim irange As Range, h As Range, l As Range
Set irange = Range("A2:An77")
If Not Application.Intersect(Target, irange) Is Nothing And Target.Columns.Count = 1 And Target.Rows.Count = 1 Then
If H1 <> "" And L1 <> "" Then
For Each hfh In Cells(H1, 1).Resize(1, irange.Columns.Count)
hn = hn + 1
hfh.Interior.Color = arrH(hn)
Next
For Each hfl In Cells(2, L1).Resize(irange.Rows.Count, 1)
Ln = Ln + 1
hfl.Interior.Color = arrL(Ln)
Next
End If
For Each h In Cells(Target.Row, 1).Resize(1, irange.Columns.Count)
n1 = n1 + 1
ReDim Preserve arrH(1 To n1)
arrH(n1) = h.Interior.Color
Next
For Each l In Cells(2, Target.Column).Resize(irange.Rows.Count, 1)
n2 = n2 + 1
ReDim Preserve arrL(1 To n2)
arrL(n2) = l.Interior.Color
Next
H1 = Target.Row
L1 = Target.Column

Cells(Target.Row, 1).Resize(1, irange.Columns.Count).Interior.ColorIndex = 37
Cells(2, Target.Column).Resize(irange.Rows.Count, 1).Interior.ColorIndex = 35

End If
End Sub
Sub 开关高亮()
Set irange = Range("A2:An77")
If H1 <> "" And L1 <> "" Then
For Each hfh In Cells(H1, 1).Resize(1, irange.Columns.Count)
hn = hn + 1
hfh.Interior.Color = arrH(hn)
Next
For Each hfl In Cells(2, L1).Resize(irange.Rows.Count, 1)
Ln = Ln + 1
hfl.Interior.Color = arrL(Ln)
Next
End If
If yn Then
yn = False
Else
yn = True
End If
End Sub

网友(2):

若原背景为单色,VBA编程可行。若背景有多种颜色则难办了。