Excel 保留第一个三行并删除其余的重复项
Excel keep 1st three rows and delete rest of duplicates
我有 20K 数据 sheet,其中包含第 1 列的多个重复项。我需要为每个数字保留第 1 列的第一个三个重复项,并删除其余部分。
我需要保持黄色突出显示并删除其余部分。
因此,按照 VBA 路线,您需要遍历选项卡中的每一行并测试该值以查看它是否重复,如果重复则增加一个计数器变量,一旦该计数器变量达到 3,您就开始删除行。
如果您没有使用过 VBA,这会有点复杂。请花一些时间玩代码并理解它。我在里面写了评论来帮忙。
Sub keepFirstThreeDuplicates()
Dim workingRow As Integer
Dim currentDup As String
Dim dupCounter As Integer
Dim wsheet As Worksheet
'change this to your tab name
Set wsheet = ThisWorkbook.Sheets("Sheet1")
'loop through every row just guessing that your data starts at row 1 (A1) and goes to 50000 (A50000)
For workingRow = 1 To 50000
If workingRow = 1 Then 'we are at the first row, so grab the value and set dupCounter to 1
currentDup = wsheet.Cells(workingRow, 1).Value 'Assuming column 1, so this is Cell A1
dupCounter = 1
ElseIf currentDup = wsheet.Cells(workingRow, 1).Value Then 'we have another duplicate
If dupCounter = 3 Then 'We already have three duplicates, so delete the row, and set the row back one (because we deleted the row)
wsheet.Rows(workingRow).Delete
workingRow = workingRow - 1
Else
dupCounter = dupCounter + 1
End If
Else 'We are at a new value, so grab the value and set dupCounter to 1
currentDup = wsheet.Cells(workingRow, 1).Value
dupCounter = 1
End If
'exit the for loop if we hit a blank
If currentDup = "" Then Exit For
Next workingRow
End Sub
如果您是 VBA 的超级新手,请使用此代码:
在您的工作簿中,按 Ctrl+F11 进入 Visual Basic
编辑器 (VBE)
您的工作簿将在 VBA 项目面板中称为 "VBAProject"。
右击它 select 插入>>模块
双击您的新模块 "Module1" 打开它。
粘贴此代码。
到运行它,点击代码中的某处并点击播放按钮
顶部(或键盘上的 F5)。确保你编辑代码以适应
您的工作簿的需要,例如将选项卡名称更改为您的选项卡。
还请确保在 运行 像这样将工作簿备份之前对其进行备份
将删除行,您将无法撤消。
最后,这只是一种方法。这并不意味着 100% 满足您的需求,因为我在猜测行数,您的数据在哪一列,数据已经按您的重复列排序,以及其他诸如此类的东西。这只是为了在 VBA 中为您指明一个坚实的方向。它在我的测试工作簿上工作,用于我在新工作簿的 A 列中创建的假列表。
另一种不使用 VBA 并且不需要对数据进行排序的方法是创建第三列数据,您只需计算发件人 ID 出现的次数。
Assuming:
Sender is column B
Message is column C
Count will be new column C
Row 1 is a header row
Data starts in row 2
在 D2 中放置以下公式:
=COUNTIF($B:B2,B2)
将其复制到整列。
Select select 在您的数据中或在您的 header 行中。
右键单击 D1 中的 header 和 select 过滤器,然后按单元格值弹出 select。 (您的数据可能会消失,但它只是隐藏了)
左键单击 D1 和 select 数字过滤器中的漏斗图标。从飞出select小于等于并把数字3放在右上角的方框里。
Select 您原来的 2 列数据并复制并粘贴到新的 sheet,您将只有前三个条目在新列表中。
我有 20K 数据 sheet,其中包含第 1 列的多个重复项。我需要为每个数字保留第 1 列的第一个三个重复项,并删除其余部分。
我需要保持黄色突出显示并删除其余部分。
因此,按照 VBA 路线,您需要遍历选项卡中的每一行并测试该值以查看它是否重复,如果重复则增加一个计数器变量,一旦该计数器变量达到 3,您就开始删除行。
如果您没有使用过 VBA,这会有点复杂。请花一些时间玩代码并理解它。我在里面写了评论来帮忙。
Sub keepFirstThreeDuplicates()
Dim workingRow As Integer
Dim currentDup As String
Dim dupCounter As Integer
Dim wsheet As Worksheet
'change this to your tab name
Set wsheet = ThisWorkbook.Sheets("Sheet1")
'loop through every row just guessing that your data starts at row 1 (A1) and goes to 50000 (A50000)
For workingRow = 1 To 50000
If workingRow = 1 Then 'we are at the first row, so grab the value and set dupCounter to 1
currentDup = wsheet.Cells(workingRow, 1).Value 'Assuming column 1, so this is Cell A1
dupCounter = 1
ElseIf currentDup = wsheet.Cells(workingRow, 1).Value Then 'we have another duplicate
If dupCounter = 3 Then 'We already have three duplicates, so delete the row, and set the row back one (because we deleted the row)
wsheet.Rows(workingRow).Delete
workingRow = workingRow - 1
Else
dupCounter = dupCounter + 1
End If
Else 'We are at a new value, so grab the value and set dupCounter to 1
currentDup = wsheet.Cells(workingRow, 1).Value
dupCounter = 1
End If
'exit the for loop if we hit a blank
If currentDup = "" Then Exit For
Next workingRow
End Sub
如果您是 VBA 的超级新手,请使用此代码:
在您的工作簿中,按 Ctrl+F11 进入 Visual Basic 编辑器 (VBE)
您的工作簿将在 VBA 项目面板中称为 "VBAProject"。 右击它 select 插入>>模块
双击您的新模块 "Module1" 打开它。
粘贴此代码。
到运行它,点击代码中的某处并点击播放按钮 顶部(或键盘上的 F5)。确保你编辑代码以适应 您的工作簿的需要,例如将选项卡名称更改为您的选项卡。 还请确保在 运行 像这样将工作簿备份之前对其进行备份 将删除行,您将无法撤消。
最后,这只是一种方法。这并不意味着 100% 满足您的需求,因为我在猜测行数,您的数据在哪一列,数据已经按您的重复列排序,以及其他诸如此类的东西。这只是为了在 VBA 中为您指明一个坚实的方向。它在我的测试工作簿上工作,用于我在新工作簿的 A 列中创建的假列表。
另一种不使用 VBA 并且不需要对数据进行排序的方法是创建第三列数据,您只需计算发件人 ID 出现的次数。
Assuming:
Sender is column B
Message is column C
Count will be new column C
Row 1 is a header row
Data starts in row 2
在 D2 中放置以下公式:
=COUNTIF($B:B2,B2)
将其复制到整列。
Select select 在您的数据中或在您的 header 行中。
右键单击 D1 中的 header 和 select 过滤器,然后按单元格值弹出 select。 (您的数据可能会消失,但它只是隐藏了)
左键单击 D1 和 select 数字过滤器中的漏斗图标。从飞出select小于等于并把数字3放在右上角的方框里。
Select 您原来的 2 列数据并复制并粘贴到新的 sheet,您将只有前三个条目在新列表中。