豆搜网 文档下载 文档下载导航
设为首页 | 加入收藏
搜索 请输入内容:  
 导航当前位置: 文档下载 > 所有分类 > vba
侵权投诉

vba

uuu

Sub 删除重复行()
Dim x As Integer
AA = InputBox("请输入需判断重复数据的所在列" & Chr(10) & "请输入列号字符如A,B,C", "提示:")
If AA = "" Then Exit Sub '返回值不为“是”,则退出
x = 2
i = Range(AA & 65536).End(xlUp).Row

Do While x < i
If Application.WorksheetFunction.CountIf(Range(Cells(1, AA), Cells(x - 1, AA)), Cells(x, AA)) <> 1 Then
x = x + 1
Else
Range(Cells(x, 1), Cells(x, 256)).Select
Selection.Delete Shift:=xlUp
End If
Loop
MsgBox "删除重复完毕"
Range("A1").Select
End Sub








Excel中删除重复数据(用VBA代码)


第一种情况保留不重复的记录行,重复的只保留一行。
1、打开有重复数据的EXCEL
2、Alt+F11 打开宏的VB编辑器
3、左边双击:ThisWorkBook
4、贴入以下代码并运行即可:
Sub 删除重复数据()
'删除col列的重复数据
'本例是删除标题为sheet1的EXCEL表中A列(从A2单元格开始)的重复数据
Application.ScreenUpdating = False
'可根据实际情况修改下面三行的结尾值
Dim sheetsCaption As String: sheetsCaption = "Sheet1"
Dim Col As String: Col = "A"
Dim StartRow As Integer: StartRow = 2
'以下不需要修改
Dim EndRow As Integer: EndRow = Sheets(sheetsCaption).Range(Col & "65536").End(xlUp).Row
Dim Count_1 As Integer: Count_1 = 0
Dim count_2 As Integer: count_2 = 0
Dim i As Integer: i = StartRow
With Sheets(sheetsCaption)
Do
Count_1 = Count_1 + 1
For j = StartRow To i - 1
If .Range(Col & i) = .Range(Col & j) Then
Count_1 = Count_1 - 1
.Range(Col & i).EntireRow.Delete
EndRow = Sheets(sheetsCaption).Range(Col & "65536").End(xlUp).Row
i = i - 1
count_2 = count_2 + 1
Exit For
End If
Next
i = i + 1
Loop While i < EndRow + 1
End With
MsgBox "共有" & Count_1 & "条不重复的数据"
MsgBox "删除" & count_2 & "条重复的数据"
Application.ScreenUpdating = True
End Sub
5、按F5键运行即可

====================================分段======================================
第二种情况:先删除不重记录行,然后保留一行重复的,代码如下:

Private Sub CommandButton1_Click()

Dim 提示信息
Dim 最后行号
Dim 循环计数
Dim 重复数
Dim 筛选列
Dim 升降序

'根据需要设定筛选列
筛选列 = "B"

'禁止屏幕刷新
Application.ScreenUpdating = False

提示信息 = MsgBox("先删除不重复的行吗?", vbOKCancel, "警告:")

If 提示信息 = 1 Then
'先删除不重复的
最后行号 = Range(筛选列 & "65536").End(xlUp).Row
For 循环计数 = 最后行号 To 2 Step -1 '不处理首行的标题栏
重复数 = Application.WorksheetFunction.CountIf(Range(筛选列 & ":" & 筛选列), Range(筛选列 & Format(循环计数))) 'vba中调用Excel内置函数CountIf()
If 重复数 = 1 Then
Rows(Format(循环计数) & ":" & Format(循环计数)).Delete
End If
Next 循环计数
End If

'再删除重复的(保留1行)

第1页

TOP相关主题

热门文档

站点地图 | 文档上传 | 侵权投诉 | 手机版
新浪认证  诚信网站  绿色网站  可信网站   非经营性网站备案
本站所有资源均来自互联网,本站只负责收集和整理,均不承担任何法律责任,如有侵权等其它行为请联系我们.
文档下载 Copyright 2013 doc.docsou.com All Rights Reserved.  闽ICP备15022310号-9  闽公网安备 35021102001881号  email
返回顶部