|
|
Sub 查询工作表内单元格有没有重复值()
Dim d As New Dictionary, dizhi As Range, r As New RegExp, dizhi1 As Range
'两次使用正则表达式判定输入的格式是否正确
'Set r = CreateObject("VBScript.RegExp")
kaishi:
quyu = InputBox("请输入活动工作表的一个单元格(如a1,用currentregion)或输入一个区域(b2:c390):", "请输入", "b2:c101")
Stop
r.Global = True
r.Pattern = "^[a-zA-Z]\d+$"
If r.Test(quyu) Then linshi = 3 Else linshi = 0
r.Pattern = "^[a-zA-Z]\d+:[a-zA-Z]\d+$"
If Not (r.Test(quyu)) And linshi = 0 Then MsgBox "输入格式错误,请重新输入": GoTo kaishi
If r.Test(quyu) Then
Range(quyu).Select
MsgBox "请查看选中的区域"
arr = Range(quyu)
For hang = LBound(arr) To UBound(arr)
For lie = LBound(arr, 2) To UBound(arr, 2)
If d.Exists(arr(hang, lie)) And arr(hang, lie) <> "" Then
Set dizhi = Range(quyu).Find(arr(hang, lie))
Set dizhi1 = Range(quyu).CurrentRegion.FindNext(dizhi)
MsgBox "存在重复单元格,查询到的第一个重复单元格内容为""" & arr(hang, lie) & _
""";地址为" & dizhi.Address(0, 0) & "和" & dizhi1.Address(0, 0): Union(dizhi, dizhi1).Select: Exit Sub
Else
d(arr(hang, lie)) = ""
End If
Next lie
Next hang
' Range(quyu).Interior.Pattern = xlPatternNone
Else
Range(quyu).CurrentRegion.Select
MsgBox "请查看选中的颜色区域"
arr = Range(quyu).CurrentRegion
For hang = LBound(arr) To UBound(arr)
For lie = LBound(arr, 2) To UBound(arr, 2)
If d.Exists(arr(hang, lie)) And arr(hang, lie) <> "" Then
Set dizhi = Range(quyu).CurrentRegion.Find(arr(hang, lie))
Set dizhi1 = Range(quyu).CurrentRegion.FindNext(dizhi)
MsgBox "存在重复单元格,查询到的第一个重复单元格内容为""" & arr(hang, lie) & _
""";地址为" & dizhi.Address(0, 0) & "和" & dizhi1.Address(0, 0): Union(dizhi, dizhi1).Select: Exit Sub
Else
d(arr(hang, lie)) = ""
End If
Next lie
Next hang
' Range(quyu).CurrentRegion.Interior.Pattern = xlPatternNone
End If
MsgBox "恭喜,未发现重复单元格,代码结束!!"
End Sub
|
|