如何根据重复的列值对 excel 列进行着色
How color excel column based on duplicate cloumn values
我有一个名为 order# 的 Excel 列,该列的订单号具有重复值,因为每个订单都有一个或多个项目,因此如果订单有 3 个项目,则 order# 将重复三个次等等。
我想用两种颜色来区分不同的订单,所以如果第一个订单#是“1”,它将是红色,第二个“2”将是黄色,第三个“3”将再次是红色, 第四个“4”为黄色,依此类推。
+----------+--------+
| order# | item# |
+----------+--------+
| 1 | 11 |
| 2 | 12 |
| 2 | 22 |
+----------+--------+
这应该可以解决问题:
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
保留条件格式,例如:
=MOD(ROUND(SUM(1/COUNTIF($A:$A2,$A:$A2)),0),2)=0
'and the other color
=MOD(ROUND(SUM(1/COUNTIF($A:$A2,$A:$A2)),0),2)=1
不需要 vba 或 运行 宏,每次在列表中更改内容时,复制范围公式,如 A2:B100
单击要更改颜色的列。选择格式和格式列。您可以在一处编辑或自定义所有属性。 Excel templates 提供大量使用 Microsoft Excel 设计的模板。希望这篇文章能帮助您理解 excel 与预设计模板的关系。
我有一个名为 order# 的 Excel 列,该列的订单号具有重复值,因为每个订单都有一个或多个项目,因此如果订单有 3 个项目,则 order# 将重复三个次等等。
我想用两种颜色来区分不同的订单,所以如果第一个订单#是“1”,它将是红色,第二个“2”将是黄色,第三个“3”将再次是红色, 第四个“4”为黄色,依此类推。
+----------+--------+
| order# | item# |
+----------+--------+
| 1 | 11 |
| 2 | 12 |
| 2 | 22 |
+----------+--------+
这应该可以解决问题:
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
保留条件格式,例如:
=MOD(ROUND(SUM(1/COUNTIF($A:$A2,$A:$A2)),0),2)=0
'and the other color
=MOD(ROUND(SUM(1/COUNTIF($A:$A2,$A:$A2)),0),2)=1
不需要 vba 或 运行 宏,每次在列表中更改内容时,复制范围公式,如 A2:B100
单击要更改颜色的列。选择格式和格式列。您可以在一处编辑或自定义所有属性。 Excel templates 提供大量使用 Microsoft Excel 设计的模板。希望这篇文章能帮助您理解 excel 与预设计模板的关系。