|
hacker85 发表于 2014-12-3 09:49
@gaoch35 我觉得你没有看懂Vicel的话,他是让你把你所有要求用1、2、3……的形式,一条一条的叙述清楚,并 ...
区域可自由更改,能处理交叉单元格问题,可选择清空或删除多余重复单元格
- Sub SC()
-
- Dim rng As Range, cell As Range, d As Object, dd As Object, s As Integer, n As Integer, Z As String, T As String
- Set d = CreateObject("scripting.dictionary")
- Set dd = CreateObject("scripting.dictionary")
- Application.ScreenUpdating = False
- rng1 = "A1: C5"
- rng2 = "C2: E9"
- rng3 = "A22:E28"
- Union(Range(rng1), Range(rng2), Range(rng3)).Select
- s = 0: n = 0
- For Each cell In Selection
- Z = cell.Address(0, 0)
- T = cell.Value
- If Not d.exists(Z) Then
- s = s + 1
- d.Add Z, T
- If T = "" Then
- GoTo 1 '单元格值为空直接往下一单元格
- ElseIf Trim(T) <> "" Then
- If Not dd.exists(T) Then
- dd.Add T, Z
- K = dd.Item(T)
- ElseIf Z <> dd.Item(T) Then
- n = n + 1
- If rng Is Nothing Then
- Set rng = cell
- Else
- Set rng = Union(rng, cell)
- End If
- End If
- Else
- cell.Value = ""
- End If
- Else
- GoTo 1 '单元格为已查询过的,直接往下一单元格
- End If
- 1: Next
- d.RemoveAll
- rng.Select
- Selection.Delete Shift:=xlUp '删除重复单元格,或换下面代码清空重复单元格
- 'Selection.Clear
- 'MsgBox "共选择了" & s & "个单元格,其中" & n & "个重复单元格已被清空"
- Application.ScreenUpdating = True
-
- End Sub
复制代码
或直接在表格里选择所需操作区域,再执行下列代码
- Sub SC()
-
- Dim rng As Range, cell As Range, d As Object, dd As Object, s As Integer, n As Integer, Z As String, T As String
- Set d = CreateObject("scripting.dictionary")
- Set dd = CreateObject("scripting.dictionary")
- Application.ScreenUpdating = False
- s = 0: n = 0
- For Each cell In Selection
- Z = cell.Address(0, 0)
- T = cell.Value
- If Not d.exists(Z) Then
- s = s + 1
- d.Add Z, T
- If T = "" Then
- GoTo 1 '单元格值为空直接往下一单元格
- ElseIf Trim(T) <> "" Then
- If Not dd.exists(T) Then
- dd.Add T, Z
- K = dd.Item(T)
- ElseIf Z <> dd.Item(T) Then
- n = n + 1
- If rng Is Nothing Then
- Set rng = cell
- Else
- Set rng = Union(rng, cell)
- End If
- End If
- Else
- cell.Value = ""
- End If
- Else
- GoTo 1 '单元格为已查询过的,直接往下一单元格
- End If
- 1: Next
- d.RemoveAll
- rng.Select
- Selection.Delete Shift:=xlUp '删除重复单元格,或换下面代码清空重复单元格
- 'Selection.Clear
- 'MsgBox "共选择了" & s & "个单元格,其中" & n & "个重复单元格已被清空"
- Application.ScreenUpdating = True
-
- End Sub
复制代码
删除重复.rar
(9.4 KB, 下载次数: 18)
|
评分
-
1
查看全部评分
-
|