在这种情况下如何删除Excel行数据

时间:2016-11-03 18:57:02

标签: excel vba row delete-row

我有以下Excel数据:

----|----|----|--
      A         B
----|----|----|--
      C    D
----|----|----|--
      B    A
----|----|----|--

并在我的需要第一排" A B"等于第三行" B A",我只想保留其中一个。如何删除另一个,THx

1 个答案:

答案 0 :(得分:0)

我可以按如下方式测试和修改代码:

Option Explicit

Sub MAIN()
    Dim dataRng As Range, col As Range, cell As Range

    With Worksheets("myWorksheetName") '<--| change "myWorksheetName" to your actual worksheet name
        With .UsedRange
            Set dataRng = .Cells
            With .Offset(1, .Columns.Count + 1).Resize(.Columns.Count, .Rows.Count)
                .Value = Application.Transpose(dataRng.Value)
                For Each col In .Columns
                    col.Sort key1:=col.Cells(1, 1), order1:=xlAscending, Header:=xlNo
                    col.Cells(1, 1).Offset(-1) = Replace("|" & Join(Application.Transpose(col.Value), "|") & "|", "||", "")
                Next col

                For Each cell In .Rows(1).Offset(-1, 1).Resize(, .Columns.Count - 1).Cells
                    If WorksheetFunction.CountIf(.Rows(1).Offset(-1).Resize(, cell.Column - .Columns(1).Column), cell.Value) > 0 Then .Offset(, -1).Cells(cell.Column - .Columns(1).Column, 1) = 1
                Next cell
                .Offset(-1).Resize(.Rows.Count + 1).ClearContents
            End With

            With .Resize(, .Columns.Count + 1)
                .AutoFilter Field:=.Columns.Count, Criteria1:=1
                .Resize(, 1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
            End With
        End With
    End With
End Sub