EXCEL VBA范围内填充行或单元颜色

1.数据范围为k:q列 这个范围可以自由修改
2025-04-04 16:35:25
推荐回答(3个)
回答1:

Sub s()
    Set rg = [k:q]
    c = 20
    x = rg.Column
    y = x + rg.Columns.Count - 1
    n = Cells(Rows.Count, x).End(3).Row
    For i = n To n - c + 1 Step -1
        If i = 1 Then Exit For
        f = True
        For j = x To y
            If Cells(i, j) = Cells(i - 1, j) Then
                f = False
                Cells(i, j).Interior.ColorIndex = 7
            End If
        Next
        If f Then Range(Cells(i, x), Cells(i, y)).Interior.ColorIndex = 6
    Next
End Sub

回答2:

VBA代码如下:
Sub s()
Set rg = [k:q]
c = 20
x = rg.Column
y = x + rg.Columns.Count - 1
n = Cells(Rows.Count, x).End(3).Row
For i = n To n - c + 1 Step -1
If i = 1 Then Exit For
f = True
For j = x To y
If Cells(i, j) = Cells(i - 1, j) Then
f = False
Cells(i, j).Interior.ColorIndex = 7
End If
Next
If f Then Range(Cells(i, x), Cells(i, y)).Interior.ColorIndex = 6
Next
End Sub

回答3:

Range("A1:A2").Interior.ColorIndex = 3

类似这种,只需要去更改range区域即可!