VBA:将Excel表的重复列值的第一行和最后一行保持
我有一个带有20k行的Excel工作表:
Header1 | Header2 |
---|---|
1 | P |
2 | P |
2 | P 3 P |
4 | Q |
5 | R |
6 | R |
7 | R 7 R |
8 | R |
9 | S |
10 | S |
我想要一个VBA代码来删除包含重复的行,但保持重复的第一行。结果应该是这样的:
header1 | header2 |
---|---|
1 | p |
3 | p |
4 | q |
5 | r |
8 | r |
9 | s |
10 | s |
我修改了以下代码在这里选择包含列Header2中的重复项的范围。
Sub Delete_Dups_Keep_Last_v2()
Dim SelRng As Range
Dim Cell_in_Rng As Range
Dim RngToDelete As Range
Dim SelLastRow As Long
Application.DisplayAlerts = False
Set SelRng = Application.InputBox("Select cells", Type:=8)
On Error GoTo 0
Application.DisplayAlerts = True
SelLastRow = SelRng.Rows.Count + SelRng.Row - 1
For Each Cell_in_Rng In SelRng
If Cell_in_Rng.Row < SelLastRow Then
If Cell_in_Rng.Row > SelRng.Row Then
If Not Cell_in_Rng.Offset(1, 0).Resize(SelLastRow - Cell_in_Rng.Row).Find(What:=Cell_in_Rng.Value, Lookat:=xlWhole) Is Nothing Then
'this value exists again in the range
If RngToDelete Is Nothing Then
Set RngToDelete = Cell_in_Rng
Else
Set RngToDelete = Application.Union(RngToDelete, Cell_in_Rng)
End If
End If
End If
End If
Next Cell_in_Rng
If Not RngToDelete Is Nothing Then RngToDelete.EntireRow.Delete
End Sub
另一个代码找到在这里=“不确定如何标记某人”> Ash 使用字典自动选择手动选择和速度,但无法产生所需的结果。
Sub keepFirstAndLast()
Dim toDelete As Range: Set toDelete = Sheet1.Rows(999999) '(to not start with a null range)
Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
Dim a As Range
For Each a In Sheet1.Range("B2", Sheet1.Range("B999999").End(xlUp))
If Not dict.Exists(a.Value2) Then
dict(a.Value2) = 0 ' first appearence, dont save the row
Else
' if last observed occurrence was a duplicate, add it to deleted range
If dict(a.Value2) > 0 Then Set toDelete = Union(toDelete, Sheet1.Rows(dict(a.Value2)))
dict(a.Value2) = a.row ' not first appearence, save the row for eventual deletion
End If
Next
toDelete.Delete
End Sub
I have an Excel worksheet with 20K rows like this:
header1 | header2 |
---|---|
1 | P |
2 | P |
3 | P |
4 | Q |
5 | R |
6 | R |
7 | R |
8 | R |
9 | S |
10 | S |
I want a VBA code to delete the rows containing duplicates, but keep the first and last row of the duplicates. The result should be like this:
header1 | header2 |
---|---|
1 | P |
3 | P |
4 | Q |
5 | R |
8 | R |
9 | S |
10 | S |
I have modified the following code found here to do just that, but every time I have to manually select the range containing the duplicates in column header2.
Sub Delete_Dups_Keep_Last_v2()
Dim SelRng As Range
Dim Cell_in_Rng As Range
Dim RngToDelete As Range
Dim SelLastRow As Long
Application.DisplayAlerts = False
Set SelRng = Application.InputBox("Select cells", Type:=8)
On Error GoTo 0
Application.DisplayAlerts = True
SelLastRow = SelRng.Rows.Count + SelRng.Row - 1
For Each Cell_in_Rng In SelRng
If Cell_in_Rng.Row < SelLastRow Then
If Cell_in_Rng.Row > SelRng.Row Then
If Not Cell_in_Rng.Offset(1, 0).Resize(SelLastRow - Cell_in_Rng.Row).Find(What:=Cell_in_Rng.Value, Lookat:=xlWhole) Is Nothing Then
'this value exists again in the range
If RngToDelete Is Nothing Then
Set RngToDelete = Cell_in_Rng
Else
Set RngToDelete = Application.Union(RngToDelete, Cell_in_Rng)
End If
End If
End If
End If
Next Cell_in_Rng
If Not RngToDelete Is Nothing Then RngToDelete.EntireRow.Delete
End Sub
Another code found here by user A.S.H. automates the manual selection and speed by using Dictionary, but fails to produce the wanted result.
Sub keepFirstAndLast()
Dim toDelete As Range: Set toDelete = Sheet1.Rows(999999) '(to not start with a null range)
Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
Dim a As Range
For Each a In Sheet1.Range("B2", Sheet1.Range("B999999").End(xlUp))
If Not dict.Exists(a.Value2) Then
dict(a.Value2) = 0 ' first appearence, dont save the row
Else
' if last observed occurrence was a duplicate, add it to deleted range
If dict(a.Value2) > 0 Then Set toDelete = Union(toDelete, Sheet1.Rows(dict(a.Value2)))
dict(a.Value2) = a.row ' not first appearence, save the row for eventual deletion
End If
Next
toDelete.Delete
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
简单解决方案:
Simple solution:
您可能会从
specialcells
中受益,可以根据公式选择这些行:[![Enter Image Description在此处] [1]] [1] [1]
棘手的部分在这里:
第一行将创建,如果(OR)公式以检查该行是否必须删除。它将返回
x
如果不是,else0
第二行只有在包含一个数字时才会删除整个行(零)
[1]: https://i.sstatic.net/ulhti.gif
You may benefit from
SpecialCells
to select those rows based on formula:[![enter image description here][1]][1]
The tricky part is here:
First line will create and IF(OR) formula to check if the row must be deleted or not. It will return
x
if not, else0
Second line will delete entire rows only if it contains a number (zero)
[1]: https://i.sstatic.net/UlhtI.gif
这也可以使用电源查询来完成,该电源查询可在Windows Excel 2010+和Excel 365(Windows或Mac)中使用,
以使用电源查询
data =&gt;获取&amp; transform =&gt;从表/范围
或从表格中
home =&gt;高级编辑器
应用步骤
以了解算法m代码
This can also be accomplished using Power Query, available in Windows Excel 2010+ and Excel 365 (Windows or Mac)
To use Power Query
Data => Get&Transform => from Table/Range
orfrom within sheet
Home => Advanced Editor
Applied Steps
to understand the algorithmM Code
在排序范围内保持第一和最后
Keep First and Last In Sorted Range