excel vba查找填充单元格颜色

1.。第11列到25列(为列范围)
2.。第20行到最后有内容单元行(为行范围)
3.。同列查找(从最后行向上查找至第20行结束)
4.。同列上下单元格内容相同时填充单元格为青色(这里的上下单元格指的是3个单元格中间单元格勿略后的上下单元)

Public Sub 条件填充颜色()

Dim H As Long, L As Long, maxH As Long

For L = 11 To 25

    maxH = Cells(Rows.Count, L).End(xlUp).Row

    For H = maxH To 20 Step -1

        If Cells(H, L).Value = Cells(H + 1, L).Value And Cells(H, L).Value = Cells(H - 1, L).Value Then

            Cells(H, L).Interior.ColorIndex = 42

        End If

    Next

Next

End Sub

追问

差不多是这样的意思,可能是我表达有误
例如
倒数第一为A(填充为“青色”)
倒数第二为C(填充为黄色)
倒数第三为A(填充为“青色”)

追答

追问

cfzzj007老师:请问如何修改代码才对

追答

不明白,AC是什么意思。

追问

倒数第一为A(填充为“青色”)
倒数第二为C(填充为黄色)
倒数第三为A(填充为“青色”)
不明白,AC是什么意思。表示倒数第一与倒数第三相同时(填充为“青色”),倒数第二为的内容(填充为黄色)

追答Public Sub 条件填充颜色()
Dim H As Long, L As Long, maxH As Long
For L = 11 To 25
    maxH = Cells(Rows.Count, L).End(xlUp).Row
    For H = maxH To 20 Step -1
        If Cells(H, L).Value <> Cells(H + 1, L).Value And Cells(H+1, L).Value = Cells(H - 1, L).Value Then
            Cells(H, L).Interior.ColorIndex = 42
            Cells(H-1, L).Interior.ColorIndex = 6 
            Cells(H+1, L).Interior.ColorIndex = 6             
         End If
    Next
Next
End Sub

温馨提示:答案为网友推荐,仅供参考
相似回答