【发布时间】:2017-02-08 19:01:50
【问题描述】:
我在工作中经常需要处理大型 Excel 文件,因此我决定第一次使用 VBA 来让我的生活更轻松。 我在互联网上找到了一个代码,修改并适应了我自己的工作表,它工作正常。
我有一列 ID,我需要找到重复 ID 的范围并将它们分组。到目前为止,我是手动完成的,而且花了很长时间。此代码有效地为重复的范围着色,但不会在每次出现新范围时切换为不同的颜色。
假设我有这个: 001 001 001 002 002
它将 001 着色为蓝色,并将 002 着色为蓝色。并非总是如此,但它会发生,我不希望这样。
代码如下:
Sub ColourDuplicates()
Dim Rng As Range
Dim Cel As Range
Dim Cel2 As Range
Dim Colour As Long
Set Rng = Worksheets("Sheet1").Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
Rng.Interior.ColorIndex = xlNone
Colour = 6
For Each Cel In Rng
If WorksheetFunction.CountIf(Rng, Cel) > 1 And Cel.Interior.ColorIndex = xlNone Then
Set Cel2 = Rng.Find(Cel.Value, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False, SearchDirection:=xlNext)
If Not Cel2 Is Nothing Then
Firstaddress = Cel2.Address
Do
Cel.Interior.Color = Colour
Cel2.Interior.Color = Colour
Set Cel2 = Rng.FindNext(Cel2)
Loop While Firstaddress <> Cel2.Address
End If
Colour = Colour + 1
End If
Next
End Sub
那么,是否有可能在每次出现一组新值时将其更改为不同的颜色?
非常感谢!
【问题讨论】:
标签: vba excel colors duplicates