作者:321 | 来源:互联网 | 2023-06-21 12:24
RemoveDuplicates后边必须跟参数否则不起作用ActiveSheet.Range(G21:R36).RemoveDuplicatesColumns:12,Header:xl
RemoveDuplicates后边必须跟参数否则不起作用
ActiveSheet.Range("G21:R36").RemoveDuplicates Columns:=12, Header:=xlYes
其中Columns参数指定要删除重复项的列,如果是1的话,那么第一列中重复项所在行都会被删除,如果是2则只有1,2两列均重复的行才会被删除,以此类推,你的区域12列,那么只有12列均重复的行会被删除。
如果想在12列中只根据某几列的重复来删除,比如,1 3 7列。那么参数写为Columns:=Array(1,3,7)
案例介绍:
1. 仅根据第1列重复项删除整行
源表:黄色标记的都是重复项
![](https://img.php1.cn/3cd4a/1eebe/cd5/45a090220e38e09d.webp)
VBA语句:
Option Explicit
Option Compare Text
Sub 去重()
Dim sSheet As Worksheet
Set sSheet = ActiveSheet
sSheet.Range("A1:H25").RemoveDuplicates Columns:=1, Header:=xlYes
End Sub
或:个人推荐下面这种写法
Option Explicit
Option Compare Text
Sub 去重()
Dim sSheet As Worksheet
Set sSheet = ActiveSheet
sSheet.Range("A1:A25").EntireRow.RemoveDuplicates Columns:=1, Header:=xlYes
End Sub
运行后:
![](https://img.php1.cn/3cd4a/1eebe/cd5/b428d8f746fb8d47.webp)
去重复项时忽略大小写,示例
源表:
![](https://img.php1.cn/3cd4a/1eebe/cd5/8170a21e8dddfd22.webp)
VBA语句:
Option Explicit
Option Compare Text
Sub 去重()
Dim sSheet As Worksheet
Set sSheet = ActiveSheet
sSheet.Range("A1:H25").RemoveDuplicates Columns:=1, Header:=xlYes
End Sub
运行后:
![](https://img.php1.cn/3cd4a/1eebe/cd5/7cccb7e4b6cb5cb8.webp)
RemoveDuplicates 方法去重规则总结:
a.去重时忽略大小写
b.前或后带空格的项却不做为重复项,无法去重,见上面案例。----- 关于这一点,经验验证:WPS 2019 --- 数据 --- 删除重复项 也是无法去除的,因此RemoveDuplicates 方法和工具菜单的方式去重是一样的效果。
2. 仅根据第2列重复项删除整行
源表:
![](https://img.php1.cn/3cd4a/1eebe/cd5/4fae50aeee651818.webp)
VBA语句:
Option Explicit
Option Compare Text
Sub 去重()
Dim sSheet As Worksheet
Set sSheet = ActiveSheet
sSheet.Range("A1:H25").RemoveDuplicates Columns:=2, Header:=xlYes
End Sub
或:sSheet.Range("A1:H25").RemoveDuplicates Columns:=Array(2), Header:=xlYes
运行后:
![](https://img.php1.cn/3cd4a/1eebe/cd5/ea91d84a82557da5.webp)
3. 仅根据第 1 和 2列 重复项删除整行,能否生效?
源表:
![](https://img.php1.cn/3cd4a/1eebe/cd5/6c257b6ba227cc3e.webp)
VBA语句:
Option Explicit
Option Compare Text
Sub 去重()
Dim sSheet As Worksheet
Set sSheet = ActiveSheet
sSheet.Range("A1:H25").RemoveDuplicates Columns:=Array(1, 2), Header:=xlYes
End Sub
运行后:
![](https://img.php1.cn/3cd4a/1eebe/cd5/2d903861d5ad779c.png)
发现没有效果,重复项没有去除掉!!
再示例: 要过滤的多列重复项 都处在同一行
源表:
![](https://img.php1.cn/3cd4a/1eebe/cd5/7494af3c1cda418d.webp)
VBA语句:
Option Explicit
Option Compare Text
Sub 去重()
Dim sSheet As Worksheet
Set sSheet = ActiveSheet
sSheet.Range("A1:H25").RemoveDuplicates Columns:=Array(1, 3), Header:=xlYes
End Sub
运行后:
![](https://img.php1.cn/3cd4a/1eebe/cd5/d942b7ec373849c3.webp)
第1列的重复项和第3列的重复项 处于同行,才会去做移除操作,见上图!
另:
http://www.amarindaz.com/excel-vba-remove-duplicates/ Excel VBA Remove Duplicates Method
Header- It determines whether the given range does contain header or not. User can specify three values in header parameter. They are shown below
Header:=xlYes- Choose this value if given range does contain Header
Header:=xlNo – Choose this value if given Range doesn’t contain header
Header:=xlGuess- Choose this value if you want excel to determine the existence of Header
示例1:Header:=xlYes
源表:
![](https://img.php1.cn/3cd4a/18ace/696/1d8e759bd3e6bbec.jpeg)
VBA语句:
Option Explicit
Option Compare Text
Sub 去重()
Dim sSheet As Worksheet
Set sSheet = ActiveSheet
sSheet.Range("A1:H25").RemoveDuplicates Columns:=1, Header:=xlYes
End Sub
运行后:
![](https://img.php1.cn/3cd4a/1eebe/cd5/eec57030b649a106.webp)
第1列 与标题行重复项 没有去除掉,其它行重复项已去除。
示例2:Header:=xlNo
源表:
![](https://img.php1.cn/3cd4a/1eebe/cd5/dc7ef30f57b727c7.jpeg)
VBA 语句:
Option Explicit
Option Compare Text
Sub 去重()
Dim sSheet As Worksheet
Set sSheet = ActiveSheet
sSheet.Range("A1:H25").RemoveDuplicates Columns:=1, Header:=xlNo
End Sub
运行后:
![](https://img.php1.cn/3cd4a/1eebe/cd5/0a0ce631ec450943.webp)
发现与标题行重复项已去除!
示例3:Header:=xlGuess
源表:
![](https://img.php1.cn/3cd4a/1eebe/cd5/e88efe5b0a13a7fa.webp)
VBA语句:
Option Explicit
Option Compare Text
Sub 去重()
Dim sSheet As Worksheet
Set sSheet = ActiveSheet
sSheet.Range("A1:H25").RemoveDuplicates Columns:=1, Header:=xlGuess
End Sub
运行后:
![](https://img.php1.cn/3cd4a/1eebe/cd5/8ad8f3bf8da691df.webp)