VBA 删除重复行

2025-04-08 04:29:47

1、Sub 重复行删除()Dim R As Integer '表格中行总数Dim L As Integer '表格中列总数Dim i1 As Integer '行递增事恐皇卫循环Dim i2 As Integer '行递增循环(当前行的下一行开始)Dim j As Integer '列递增循环Dim flag As BooleanR = ActiveSheet.UsedRange.Rows.CountL = ActiveSheet.UsedRange.Columns.CountDim HB As IntegerHB = 0'MsgBox R'MsgBox LFor i1 = 1 To R Step 1If (i1 + HB > R) Then '如果已经合并删除的行与已循环过的行之和大于循环前总行数则停止循环Exit ForEnd IfApplication.ScreenUpdating = FalseFor i2 = R To i1 + 1 Step -1flag = TrueFor j = 1 To L Step 1If Cells(i1, j) <> Cells(i2, j) Thenflag = FalseExit For '如果发现两行中单元格有不同,则退出本次循环End IfNext jIf flag ThenCells(i2, 1).EntireRow.DeleteHB = HB + 1 '删除一行最外层循环就减1End IfNext i2Application.ScreenUpdating = TrueNext i1End SubSub 重复行合并计算()Dim R As Integer '表格中行总数Dim L As Integer '表格中列总数Dim i1 As Integer '行递增循环Dim i2 As Integer '行递增循环(当前行的下一行开始)Dim j As Integer '列递增循环R = ActiveSheet.UsedRange.Rows.CountL = ActiveSheet.UsedRange.Columns.CountDim HB As IntegerHB = 0'MsgBox R'MsgBox LFor i1 = 1 To R Step 1If (i1 + HB > R) Then '如果已经合并计算的行与已循环过的行之和大于循环前总行数则停止循环Exit ForEnd IfFor i2 = R To i1 + 1 Step -1If Cells(i1, 1) = Cells(i2, 1) Then '选择第1列作为判断是否重复列Cells(i1, 2) = Cells(i1, 2) + Cells(i2, 2) '选择第2列作为合并计算列Cells(i2, 1).EntireRow.DeleteHB = HB + 1 '删除一行最外层循环就减1End IfNext i2Next i1End Sub

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢