如何根据重复的cloumn值来显示色彩

我有一个名为order#的Excel列,它有重复值的订单号,因为每个订单有一个或多个项目,所以如果订单有3个项目,订单#将重复三次,依此类推。

我想用两种颜色来区分不同的命令,所以如果第一个命令#是“1”就是红色,第二个“2”会是黄色,第三个“3”会再次是红色,第四个是“4” “会是黄色的,等等。

+----------+--------+ | order# | item# | +----------+--------+ | 1 | 11 | | 2 | 12 | | 2 | 22 | +----------+--------+ 

留在有条件的格式,如:

 =MOD(ROUND(SUM(1/COUNTIF($A$2:$A2,$A$2:$A2)),0),2)=0 'and the other color =MOD(ROUND(SUM(1/COUNTIF($A$2:$A2,$A$2:$A2)),0),2)=1 

没有必要vba或运行一个macros,每次你改变你的列表中的东西复制A2:B100范围的公式

这应该做的伎俩:

 Sub test_CairoCoder() Dim wS As Worksheet, _ LastRow As Long, _ ColorChg As Boolean, _ OrderNb As String Set wS = ActiveSheet ColorChg = False With wS LastRow = .Range("A" & .Rows.Count).End(xlUp).Row OrderNb = wS.Cells(2, 1) For i = 2 To LastRow If .Cells(i, 1) <> .Cells(i + 1, 1) And .Cells(i, 1) <> .Cells(i - 1, 1) Then ColorChg = Not ColorChg If ColorChg Then .Range(.Cells(i, "A"), .Cells(i + 1, "A")).Interior.Color = vbRed Else .Range(.Cells(i, "A"), .Cells(i + 1, "A")).Interior.Color = vbYellow End If Else If .Cells(i, 1) <> .Cells(i + 1, 1) Then Else If OrderNb <> .Cells(i, 1) Then OrderNb = .Cells(i, 1) ColorChg = Not ColorChg Else End If If ColorChg Then .Range(.Cells(i, "A"), .Cells(i + 1, "A")).Interior.Color = vbRed Else .Range(.Cells(i, "A"), .Cells(i + 1, "A")).Interior.Color = vbYellow End If End If End If Next i End With MsgBox "All done!", vbInformation + vbOKOnly End Sub 

尝试这个:

 Sub test() Dim i&, x&, cl As Range, Dic As Object Set Dic = CreateObject("Scripting.Dictionary"): Dic.CompareMode = vbTextCompare i = [A:A].Find("*", , xlValues, , xlByRows, xlPrevious).Row x = [1:1].Find("*", , xlValues, , xlByColumns, xlPrevious).Column For Each cl In Range("A2:A" & i) If Not Dic.exists(cl.Value2) Then Dic.Add cl.Value2, IIf(Dic.Count Mod 2 = 0, vbRed, vbYellow) End If Next cl For Each cl In Range("A2:A" & i) Range(cl, Cells(cl.Row, x)).Interior.Color = Dic(cl.Value2) Next cl End Sub 

点击你想改变颜色的列。 select格式和格式列。 您可以在一个位置编辑或自定义所有属性。 Excel模板提供了使用Microsoft Exceldevise的大量模板。 希望这可以帮助你理解与预先devise模板有关的excel。