使用VBA判断指定单元格区域中是否有重复值
写了一个VBA函数,用来判断指定单元格区域中是否有重复值。或许有用,因此辑录在此。
Function bIfUnique(rng As Range) As Boolean
Dim oDic As Object, rngCell As Range
Set oDic = CreateObject(”Scripting.Dictionary”)
For Each rngCell In rng
On Error Resume Next
oDic.Add rngCell.Text, rngCell.Text
Next rngCell
If oDic.Count <> rng.Cells.Count Then
bIfUnique = True
Else
bIfUnique = False
End If
End Function
该函数使用了Dictionary对象,其中参数rng代表要判断的单元格区域。可以使用下面的代码来测试该函数:
Sub test()
If bIfUnique(Range(”B2:C4″)) Then
MsgBox “有重复值”
Else
MsgBox “没有重复值”
End If
End Sub
