Excel 2010 VBA高亮显示不同颜色的单元格,在多个列中具有不同的重复值
如何使用不同颜色高亮显示Excel 2010中跨多个列的重复单元格 我找到了这段代码,但它只适用于一列Excel 2010 VBA高亮显示不同颜色的单元格,在多个列中具有不同的重复值,vba,excel,Vba,Excel,如何使用不同颜色高亮显示Excel 2010中跨多个列的重复单元格 我找到了这段代码,但它只适用于一列 Sub Highlight_Duplicate_Entry() Dim cel As Variant Dim myrng As Range Dim clr As Long Set myrng = Range("A2:A" & Range("A65536").End(
Sub Highlight_Duplicate_Entry()
Dim cel As Variant
Dim myrng As Range
Dim clr As Long
Set myrng = Range("A2:A" & Range("A65536").End(xlUp).Row)
myrng.Interior.ColorIndex = xlNone
clr = 3
For Each cel In myrng
If Application.WorksheetFunction.CountIf(myrng, cel) > 1 Then
If WorksheetFunction.CountIf(Range("A2:A" & cel.Row), cel) = 1 Then
cel.Interior.ColorIndex = clr
clr = clr + 1
Else
cel.Interior.ColorIndex = myrng.Cells(WorksheetFunction.Match(cel.Value, myrng, False), 1).Interior.ColorIndex
End If
End If
Next
End Sub
您需要更改范围以覆盖多列,这将导致
Match
功能失败。将其替换为Find
。下面的子项将找到指定范围内的任何重复项,并用不同的颜色突出显示它们
将代码替换为以下内容:
Sub Highlight_Duplicate_Entry()
Dim ws As Worksheet
Dim cell As Range
Dim myrng As Range
Dim clr As Long
Dim lastCell As Range
Set ws = ThisWorkbook.Sheets("Sheet1")
Set myrng = ws.Range("A2:d" & Range("A" & ws.Rows.Count).End(xlUp).Row)
With myrng
Set lastCell = .Cells(.Cells.Count)
End With
myrng.Interior.ColorIndex = xlNone
clr = 3
For Each cell In myrng
If Application.WorksheetFunction.CountIf(myrng, cell) > 1 Then
' addresses will match for first instance of value in range
If myrng.Find(what:=cell, lookat:=xlWhole, MatchCase:=False, after:=lastCell).Address = cell.Address Then
' set the color for this value (will be used throughout the range)
cell.Interior.ColorIndex = clr
clr = clr + 1
Else
' if not the first instance, set color to match the first instance
cell.Interior.ColorIndex = myrng.Find(what:=cell, lookat:=xlWhole, MatchCase:=False, after:=lastCell).Interior.ColorIndex
End If
End If
Next
End Sub
根据下面的评论添加结果的屏幕截图,以帮助澄清这是如何工作的。每组副本以单独的颜色高亮显示。不重复的值不会着色:
请在Excel中使用条件格式。条件格式将突出显示具有相同颜色的所有副本。OP希望以不同的颜色突出显示每组重复项。您好,非常感谢您的帮助,但是对于一些未突出显示的数字,代码不起作用。你能提供更多的帮助来解决我的问题吗?非常感谢,但是对于同一行中的重复数字,代码没有被理解。我的数据范围(仅数值输入)在列中有唯一的数字,但在行中没有。请立即尝试。它现在应该适用于所有需要它的情况。但是请最后一件事。我如何确定最后使用行的列,因为某些列的行数比其他列的多,然后我将例如C放入…范围(“A”&ws.rows.Count)…-第8行,因为C是包含更多行的列。非常非常感谢你!!!你做得对。很高兴它对你有用。您也可以将该部分自动化。如果您搜索“查找最后一行excel vba”,您应该会找到一些有用的选项。