乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > 怎么通过宏VBA代码删除<em>Excel</em>中有重复数据的整行?

怎么通过宏VBA代码删除<em>Excel</em>中有重复数据的整行?

作者:乔山办公网日期:

返回目录:excel表格制作


一般采用倒序的方式:例如,将D列中为“否”的整行删除:
Sub test()
For i = Cells(Rows.Count, 4).End(xltoup).Row To 1
If Range("D" & i) = "否" Then
Range("D" & i).EntireRow.Delete
End If
Next
End Sub

1、首先打开需要编辑的Excel表格,右键单击copy工作表的标签,选择打开“查看代码”。

2、然后在弹出来的窗口中点击输入:

Sub 删除重复行()

Dim xRow As Integer

Dim i As Integer

xRow = Range("B65536").End(xlUp).Row

For i = 2 To xRow

For j = i + 1 To xRow

If Cells(j, 2) = Cells(i, 2) Then

Range(Cells(j, 1), Cells(j, 256)).Rows.Delete

j = j - 1

xRow = xRow - 1

End If

Next

Next

End Sub

3、然后点击左上角的保存按钮进行保存,之后运行该代码或运行宏“删除重复行”即可。

4、然后就可以删除重复数据的整行。


Dim cel As Range, i As Integer
    For i = 1 To 100
        Set cel = Range("a" & i)
        If Application.WorksheetFunction.CountIf(Range(Range("a1:a100"), cel), cel) > 1 Then
            cel.EntireRow.Delete shift:=xlUp
            i = i - 1
        End If
    Next


字典 CreateObject("scripting.dictionary")
我的一段代码,供参考。
Set d = CreateObject("scripting.dictionary")
For Each Rng In Worksheets("原始表").Range("c1:c" & Rcount)
If Not d.exists(Rng.Value) And Worksheets("原始表").Cells(Rng.Row, 1) = 2 Then d.Add Rng.Value, ""
Next
Worksheets("支路模块矩阵").Range("b1").Resize(1, d.Count) = d.keys
Set d = Nothing

相关阅读

关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网