跨多数据块基于#Point ID列删除重复行的实现方案
函数公式方案(适用于Excel 365/2021)
假设数据满足以下前提:
- 数据块之间用空行分隔
- #Point ID列位于A列
- 数据区域为A:I列
步骤:
添加辅助列(如J列)标记数据块分组,在J1输入公式并下拉:
=IF(A1="", "", MAX($J$1:J0)+1)空行留空,同一数据块的行会获得相同组号。
在空白区域(如K1)输入公式提取去重后的数据:
=LET( rawData, A:I, groupIds, J:J, combinedData, HSTACK(groupIds, rawData), uniqueRows, UNIQUE(combinedData, FALSE, TRUE), FILTER(uniqueRows, INDEX(uniqueRows,,1)<>"", "") )公式逻辑:
HSTACK合并组号与原始数据,确保去重范围限定在同一数据块内UNIQUE(..., FALSE, TRUE)保留每个组内#Point ID首次出现的行FILTER过滤空行对应的无效组号
若数据块分隔方式不是空行,需调整辅助列的分组规则(比如根据重复表头标记组号)。
VBA模块方案(兼容全版本Excel)
以下代码自动遍历所有数据块,在每个块内保留#Point ID唯一的行,删除后续重复行:
Sub RemoveDuplicatesInEachBlock() Dim ws As Worksheet Dim lastRow As Long, currentBlockStart As Long, currentBlockEnd As Long Dim idDict As Object Dim currentRow As Long Dim idValue As Variant Set ws = ActiveSheet Set idDict = CreateObject("Scripting.Dictionary") lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row ' #Point ID在A列,可按需修改列标识 currentBlockStart = 1 ' 定位第一个数据块起始行(跳过顶部空行) Do While currentBlockStart <= lastRow And ws.Cells(currentBlockStart, "A").Value = "" currentBlockStart = currentBlockStart + 1 Loop Do While currentBlockStart <= lastRow ' 定位当前数据块结束行(找到下一个空行前一行) currentBlockEnd = currentBlockStart Do While currentBlockEnd <= lastRow And ws.Cells(currentBlockEnd + 1, "A").Value <> "" currentBlockEnd = currentBlockEnd + 1 Loop ' 从下往上遍历删除重复行(避免删除行导致索引混乱) idDict.RemoveAll For currentRow = currentBlockEnd To currentBlockStart Step -1 idValue = ws.Cells(currentRow, "A").Value If idDict.Exists(idValue) Then ws.Rows(currentRow).Delete Else idDict.Add idValue, currentRow End If Next currentRow ' 定位下一个数据块起始行(跳过块间空行) currentBlockStart = currentBlockEnd + 1 Do While currentBlockStart <= lastRow And ws.Cells(currentBlockStart, "A").Value = "" currentBlockStart = currentBlockStart + 1 Loop Loop Set idDict = Nothing Set ws = Nothing End Sub
使用说明:
- 按
Alt+F11打开VBA编辑器 - 右键工作簿名称→插入→模块,粘贴代码
- 按
Alt+F8选择RemoveDuplicatesInEachBlock执行
若#Point ID不在A列,修改代码中"A"为对应列标识;若数据块不用空行分隔,需调整块起始/结束的判断逻辑(比如检测特定表头)。
内容的提问来源于stack exchange,提问作者Anna
相关产品推荐
相关产品推荐

