Excel VBA代写如何快速标记重复值
1、如下图数据区域内含有一部分重复数据,现在我们想要快速找出重复数据并标记填充不同单元格颜色。

2、选中这列数据单元格区域

3、同时按下Alt+F11,打开vba

4、点击sheet1,然后在右边空白区域录入vba代码

5、点击【运行】,然后选择【运行子过程/用户窗体】

6、就可以发现数据中重复数据已经被标记填充不同的背景颜色了,最后我跟大家分享一下本文这里所使用的vba代码。
'激活工作表事件
'激活工作表,写入除目录外所有表名
'作者:如需Excel vba 代写,请百度方方格子
'------------------------------------------
Sub 标记选区重复值()
On Error Resume Next
Dim rn As Range, first As Range
Dim ColorIdx As Integer
Set d = CreateObject("scripting.dictionary")
Selection.Interior.ColorIndex = 2
ColorIdx = 0
For Each rn In Selection
If rn <> "" Then
If d.exists(rn.Value) Then
Set first = Range(d(rn.Value)) '第一次出现的单元格
If first.Interior.ColorIndex = 2 Then '第一次出现时 未设置过颜色
'----------------------------------
ColorIdx = (ColorIdx + 1) Mod 56 + 1 '颜色可选范围:0~56
If ColorIdx = 2 Then ColorIdx = 3
'----------------------------------
first.Interior.ColorIndex = ColorIdx
Else
ColorIdx = first.Interior.ColorIndex
End If
rn.Interior.ColorIndex = ColorIdx
Else
d.Add rn.Value, rn.Address
End If
End If
Next
End Sub
