gpt4 book ai didi

excel - 连续删除重复的单元格

转载 作者:行者123 更新时间:2023-12-04 21:39:05 24 4
gpt4 key购买 nike

只是为了澄清:
我不想删除重复的行,我想删除一行中的重复单元格

所以这是一个经典的地址表,在某些行中有重复的条目
我需要删除这些条目。
我在 VBA 中看到的大部分内容都用于删除列中的重复值,但我找不到删除行中重复值的方法。

Name  |        Address1 |       Address2 |    City |    Country

Peter | 2 foobar street |2 foobar street | Boston | USA

我希望它像:
Name  |         Address1 |  Address2 |   City  |    Country

Peter | 2 foobar street | | Boston | USA

我编写了一个宏,它将遍历所有行,然后遍历每一行的每一列,但我不知道如何在同一行的不同单元格中发现重复。

这是下面的代码:
Sub Removedupe()   
Dim LastRow As Long
Dim LastColumn As Long
Dim NextCol As Long

LastRow = Cells.Find(What:="*", After:=Range("A1"), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
LastColumn = Cells.Find(What:="*", After:=Range("A1"), SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column

For counterRow = 1 To LastRow
'I'm stuck here: how to remove a duplicate values within that row?
Next counterRow
End Sub

最佳答案

也许这会解决你的问题:

Sub RemoveDuplicatesInRow()

Dim lastRow As Long
Dim lastCol As Long
Dim r As Long 'row index
Dim c As Long 'column index
Dim i As Long

With ActiveSheet.UsedRange
lastRow = .Row + .Rows.Count - 1
lastCol = .Column + .Columns.Count - 1
End With

For r = 1 To lastRow
For c = 1 To lastCol
For i = c + 1 To lastCol 'change lastCol to c+2 will remove adjacent duplicates only
If Cells(r, i) <> "" And Cells(r, i) = Cells(r, c) Then
Cells(r, i) = ""
End If
Next i
Next c
Next r

End Sub

关于excel - 连续删除重复的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22082680/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com