excel 如何将颜色格式条件应用于单元格区域并突出显示与该条件匹配的行?

olqngx59  于 7个月前  发布在  其他
关注(0)|答案(1)|浏览(64)

我有一个CommandButton宏的代码(我在一个论坛上得到的),它可以选择工作表中的大量单元格,并根据三种按钮状态中的每一种,以不同的颜色突出显示所选单元格的背景。
我稍微调整了一下代码:

Private Sub CommandButton1_Click()
Dim Nam As Variant
Static c
Nam = Array("A", "B", "C", vbYellow, vbCyan, vbWhite)
If c = 3 Or c = "" Then c = 0
CommandButton1.Caption = Nam(c)
CommandButton1.BackColor = Nam(c + 3)

Range("A:G").Interior.ColorIndex = xlNone
Select Case Nam(c)
    Case "A": Range("A1:U1000").Interior.ColorIndex = 6
    Case "B": Range("A1:U1000").Interior.ColorIndex = 8
    Case "C": Range("A1:U1000").Interior.ColorIndex = 0
End Select
c = c + 1
End Sub

字符串
但是我需要对于每个按钮状态,它只突出显示(在三种按钮状态的每种颜色中)选择块中包含特定单元格中的值的行,这些值总是在同一列中,在每行中。
就像这样:
如果按钮状态为A,则单元格Jn(J1,J2,J3...)中包含“y”的所有行都将以A设置的颜色突出显示。类似于If(Jn =“y”),然后突出显示整行... n = n+ 1....转到下一行...重复...
如果按钮状态为B,则单元格Jn中包含“n”的所有行都将以B设置的颜色突出显示
如果按钮状态为C,则所有包含“y”或“n”的行将返回到单元格中不填充.

acruukt9

acruukt91#

在Select语句中,将ColorIndex赋给一个变量。然后循环遍历范围内的每一行,检查每一列J是否满足条件。如果满足,则将ColorIndex应用于该行。

Private Sub CommandButton1_Click()
Dim Nam As Variant
Static c
Nam = Array("A", "B", "C", vbYellow, vbCyan, vbWhite)
If c = 3 Or c = "" Then c = 0
CommandButton1.Caption = Nam(c)
CommandButton1.BackColor = Nam(c + 3)

Range("A:G").Interior.ColorIndex = xlNone

Dim NewColor As Long
Select Case Nam(c)
    Case "A": NewColor = 6
    Case "B": NewColor = 8
    Case "C": NewColor = 0
End Select

Dim rg As Range
Dim i As Long
Set rg = Range("A1:U1000")
For i = 1 To rg.Rows.Count
    If Cells(i, "J") = "y" Then Range(Cells(i, "A"), Cells(i, "U")).Interior.ColorIndex = NewColor
Next i
c = c + 1
End Sub

字符串

相关问题