第1个回答 2014-08-06
Sub tt2()
'使用字典删除重复数据,加快运行速度,并增加运行提示
Dim iCol%, i&, n&, m&, r&, d, dd, a, tmp
iCol = [B1].Column '数据源的列位置,需要的话请更改此处
Set d = CreateObject("Scripting.Dictionary") '使用字典
Set dd = CreateObject("Scripting.Dictionary") '使用字典
r = Cells(65536, iCol).End(xlUp).Row
If r < 2 Then MsgBox "只有一行数据,不需整理!": Exit Sub
a = Cells(1, iCol).Resize(r, 1)
For i = 1 To r
tmp = a(i, 1)
If Not d.Exists(tmp) Then d(tmp) = "": dd(i) = ""
Next
n = r - dd.Count
If n > 0 Then
If MsgBox("找到 " & n & " 行重复数据! 要删除吗?", vbQuestion + vbOKCancel) <> vbOK Then
Exit Sub
End If
Else
MsgBox "没有重复数据,不需整理!": Exit Sub
End If
m = 0
For i = r To 1 Step -1
If Not dd.Exists(i) Then Rows(i).Delete: m = m + 1
Next
MsgBox "找到 " & n & " 行重复数据,删除了 " & m & " 个。", vbInformation
End Sub
第2个回答 推荐于2016-10-13
Sub tt()
Dim i As Integer
For i = Range("b1").End(xlDown).Row To 1 Step -1
If Application.WorksheetFunction.CountIf(Range(Cells(1, 2), Cells(i, 2)), Cells(i, 2)) > 1 Then
Rows(i).Delete
End If
Next
End Sub
这段代码比较简单你看看
你打开excel alt+f11一起按 插入模块
复制代码 按 F5执行了看看
还有你多少行啊 我写的时候几百行 没问题 不知道几万行卡不卡呢 你看看 有问题联系我吧~
追问是这个as integer的约束吗?不行的话,我改成as long,这样行吗?
追答我是定义I 为整数 和Range("b1").End(xlDown).Row 对应最大行号的 Long应该也可以的吧
你是要嵌套在别的代码里面用还是什么情况?
本回答被提问者采纳