宏按填充颜色对行进行分组没有任何作用,我不确定为什么

时间:2019-04-05 16:31:48

标签: excel vba

我正在尝试编写一个能够根据行的填充颜色对行进行分组的宏。问题是:我的宏什么都不做。它既不对行进行分组也不抛出错误。我不确定我缺少什么。

Option Explicit

Sub RowGrouper()

Dim rng As Range
Dim lastRow As Long
    lastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row

    For Each rng In Range(Cells(10, 1), Cells(lastRow, 1)).Cells
        If rng.Interior.ColorIndex = (xlNone Or RGB(255, 255, 238)) Then
                '255,255,238 is the light yellow color in the images below
            rng.Rows.Group
        End If
    Next

End Sub

感谢您能借给我的任何帮助。

这是我要分组的行的图片,其中一个按应进行分组:

Pre-grouping Post-grouping

编辑:在迈克尔·墨菲(Michael Murphy)的帮助下,我越来越近了,但是现在只对浅黄色行进行分组,而不是对所有白色和浅黄色行进行分组。

enter image description here

1 个答案:

答案 0 :(得分:2)

您的问题在这里:

If rng.Interior.ColorIndex = (xlNone Or RGB(255, 255, 238)) Then

您需要将其更改为

If rng.Interior.ColorIndex = xlNone Or rng.Interior.Color = RGB(255, 255, 238) Then

将来,您应该尝试逐行逐步执行代码。无论单元格是什么颜色,您都可以看到If始终为False