过滤行VBA上的替代着色

嗨,我想通过在列A中的值在过滤表中交替颜色行

预期的结果是

行2-3(列A值= 1)将得到一种颜色,例如黄色

– 行4(列A值= 2)将获得另一种颜色

行5-6(A列值= 3)将再次变为黄色

我如何使用VBA在过滤的表中执行此操作?

我认为在这里着色的条件是价值是奇数或甚至。 你可以试试,

Sub AlterColour() Dim intRowCount, intLastRow, intValue, intPreValue as Integer intLastRow = Application.CountA(Sheets(1).Range("a:a")) 'Set first row to a colour Sheets(1).Range("a" & 1 ).Interior.ColorIndex = '[An Integer Here, index 36 for yellow] 'Note: inteRowCount = 2 if there is no header, otherwise set to 3 For intRowCount = 2 to intLastRow intValue = Sheets(1).Range("a" & intRowCount ).Value intPreValue = Sheets(1).Range("a" & (intRowCount - 1) ).Value If intValue = intPreValue Then Sheets(1).Range("a" & intRowCount ).Interior.ColorIndex = '[An Integer Here, index 36 for yellow] Else Sheets(1).Range("a" & intRowCount ).Interior.ColorIndex = ' [An Integer Here, index for other colour] End If Next End Sub