您现在的位置:龙卷风首页 ›› 情感日记 ›› 阅读文章

删除Excel中重复的行的小脚本

删除某项与前面重复的行的VBA脚本

目标:如果某行的X列的值跟某行的X列的值相等,则视此行为重复的行,予以删除.

脚本如下:

Sub deleteDouble()

    '用户输入
    Dim userInput
        userInput = Application.InputBox("输入需要检查的起始行,结束行,以及需要检察的列,格式如 1,20,C")
   
    Dim arrUserInput
        arrUserInput = Split(userInput, ",")
       
    Dim theColumn
        theColumn = Asc(arrUserInput(2)) - 64   '进行检验的列
       
    Dim theStart
        theStart = arrUserInput(0)  '起始行
       
    Dim theEnd
        theEnd = arrUserInput(1) '最后一行的号码.
   
    Dim i   '每一行的号码
    Dim j   '
    For i = theStart To theEnd
        For j = i + 1 To theEnd '从当前行至结尾.
            If Cells(i, theColumn) = Cells(j, theColumn) Then
                Rows(j).Delete
            End If
        Next
    Next
   
End Sub

作者 不见不散 本文仅代表作者观点,与龙卷风资讯网立场无关。

我来说两句

内容/Content