我需要具有相同列颜色的特定值的所有重复项。这将帮助我找到特定值有多少重复项,对应的列值等。请查找 Sample Excel。如果我进行条件格式,突出显示重复项,这将为所有重复项提供相同的颜色,而不管值如何。这不会解决目的。如果有的话,还建议在 excel 中区分重复值的其他方法。
问问题
4038 次
1 回答
2
没有办法用条件格式做你想做的事,但我认为下面的宏会做你想做的事。它构建了一个唯一值的字典,并为每个值分配一个随机颜色,然后匹配并重复用于任何重复项。
' Base function that can be used for different columns.
Sub colorDistinctInColumn(ByVal columnLetter As String)
Dim lastRow As Integer
Dim i As Integer
Dim dict
Dim currentCell As Range
Dim columnNumber As Integer
' Create a dictionary to hold the value/colour pairs
Set dict = CreateObject("Scripting.Dictionary")
' Find the last-used cell in the column of interest
lastRow = ActiveSheet.Columns(columnLetter).Cells.Find( _
"*", _
SearchOrder:=xlByRows, _
LookIn:=xlValues, _
SearchDirection:=xlPrevious).Row
' Pick columnNumber using the given column letter
columnNumber = ActiveSheet.Columns(columnLetter).Column
' Loop through all of the cells
For i = 1 To lastRow
Set currentCell = ActiveSheet.Cells(i, columnNumber)
' See if we've already come across the current value
If Not dict.exists(currentCell.Value) Then
' This value has not been encountered yet,
' so store it and assign a random colour
dict.Add currentCell.Value, RGB(Rnd * 255, Rnd * 255, Rnd * 255)
End If
' Set the colour of the current cell to whichever colour
' has been stored for the current cell's value
currentCell.Interior.Color = dict(currentCell.Value)
Next i
Set dict = Nothing
End Sub
' Actual Macro that will run in Excel
Sub colorDistinctInColumnA()
Call colorDistinctInColumn("A")
End Sub
于 2012-12-02T04:12:44.427 回答