简体   繁体   English

如果 A 列中的单元格等于 B 列中的单元格,则 Excel 宏将删除一行

[英]Excel Macro to delete a row if cell in Column A equals cell in Column B

Need some help here.这里需要一些帮助。 Nothing that I've searched for seems to fit my situation.我搜索的任何内容似乎都不适合我的情况。 I have a large data set where Column A and Column B have some duplicates.我有一个大数据集,其中 A 列和 B 列有一些重复项。 To clarify, it is a personnel data set with Column A being employees and column B being spouses of those employees.澄清一下,这是一个人事数据集,A 列是员工,B 列是这些员工的配偶。 However, some employees are married to each other, so I would like to delete the second instance on the list where both employees are married to each other.但是,有些员工已结婚,因此我想删除列表中两个员工都已结婚的第二个实例。 A sample of my data is this:我的数据样本是这样的:

Column A  
Kim  
Dave  
Jim  
Mary  
Mike  

Column B  
Mike  
Angela  
Susan  
Bob  
Kim

In this case, Mike is married to Kim, which is depicted in both rows 1 and 5. I want to delete row 5. It is a rather large dataset, so doing it manually would take hours with a probability of human error.在本例中,Mike 与 Kim 结婚,这在第 1 行和第 5 行中都有描述。我想删除第 5 行。这是一个相当大的数据集,因此手动操作可能需要几个小时,并且可能会出现人为错误。 Thanks!\\谢谢!\\

Edited to include my code:编辑以包含我的代码:

Sub DeleteDuplicates()

Application.ScreenUpdating = False

'Declare variables
Dim var As Variant, iSheet As Integer, iRow As Long, iRowL As Long, bln As Boolean

   'Set up the count as the number of filled rows in the first column of Sheet1.
   iRowL = Cells(Rows.Count, 1).End(xlUp).Row

   'Cycle through all the cells in that column:
   For iRow = 2 To iRowL
      'For every cell that is not empty, search through the first column in each worksheet in the
      'workbook for a value that matches that cell value.

      If Not IsEmpty(Cells(iRow, 2)) Then
         For iSheet = ActiveSheet.Index + 1 To Worksheets.Count
            bln = False
            var = Application.Match(Cells(iRow, 2).Value, ActiveSheet.Columns(9), 0)

            'If you find a matching value, clear the cell contents and exit the loop;
            'otherwise, continue searching until you reach the end of the workbook.
            If Not IsError(var) Then
               bln = True
               Exit For
            End If
         Next iSheet
      End If

      'If you do not find a matching value, do nothing, if you do find a matching value, clear the contents of the cell
      If bln = True Then
         ActiveSheet.Rows(iRow).EntireRow.Delete
      End If
   Next iRow
Application.ScreenUpdating = True

End Sub结束子

Put this formula in C1 (Assuming your data starts in A1 and B1) =COUNTIFS(A$1:A1,B1,B$1:B1,A1)将此公式放在 C1 中(假设您的数据从 A1 和 B1 开始) =COUNTIFS(A$1:A1,B1,B$1:B1,A1)

Drag down拖累

Filter data to remove none 0过滤数据以删除无 0

It works using an expanding range, counting the occurrence of the flip.它使用扩展范围工作,计算翻转的发生。 An expanding range is used to stop it from counting the original ie the one you want to keep.扩大范围用于阻止它计算原件,即您想要保留的那个。

Here is my sample data and results:这是我的示例数据和结果:

Kim     Mike    0
Bob     Mary    0
Jim     Susan   0
Mary    Bob     1
Dave    Angela  0
Mike    Kim     1

You should be able to change this to suit your needs.您应该能够更改此设置以满足您的需求。 It compares columns A & B to look for duplicates and if found, deletes them.它比较 A 和 B 列以查找重复项,如果找到,则删除它们。 It's code I previously wrote for something else but you're welcome to see if it works for you.这是我之前为别的东西写的代码,但欢迎你看看它是否适合你。

Sub FindDuplicates()

Dim i As Long, j As Long
Dim numberOfAccounts As Long, numberOfBillClasses As Long
Dim nxtRow As Long
Dim checkForRange As Range, DupeRange As Range

numberOfAccounts = Range("B" & Rows.Count).End(xlUp).Row
esrd = Range("A" & Rows.Count).End(xlUp).Row + 1

For i = 2 To numberOfAccounts
    Set checkForRange = Range("B" & i)

    For j = 1 To esrd - 1
        Set DupeRange = Range("A" & j)

        If StrComp(CStr(checkForRange.Value), CStr(DupeRange.Value), vbTextCompare) = 0 Then
            checkForRange.Interior.ColorIndex = 22
        End If
        Set DupeRange = Nothing
    Next j
    Set checkForRange = Nothing
Next i
End Sub

'This macro will delete the duplicates found after using the above macro.

Sub DeleteDuplicates()
    Dim Lastrow As Long
    Dim Lrow As Long
    Dim CalcMode As Long
    Dim ViewMode As Long

    With Application
        CalcMode = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
    End With

    Lastrow = Range("B" & Rows.Count).End(xlUp).Row
        For Lrow = Lastrow To 2 Step -1
                    If Cells(Lrow, "B").Interior.ColorIndex = 22 Then
                        Cells(Lrow, "B").Delete
                    End If
        Next Lrow

'Removes color from duplicate cells
     Columns("B:B").Select
    With Selection.Interior
        .Pattern = xlNone
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With

End Sub

You could use this formula:你可以使用这个公式:

=IFERROR(IF(AND(SUM(($A:$A&$B:$B=A1&B1)+($B:$B&$A:$A=A1&B1))>1,ROW(INDEX($B:$B,MATCH(A1,$B:$B,0)))<ROW()),"Duplicate",""),"")

In an empty column, like column C put it in C1, confirm with Ctrl-Shift-Enter.在空列中,如 C 列将其放入 C1,按 Ctrl-Shift-Enter 确认。 Then Copy down.然后复制下来。 I t will put "Duplicate" in the second version of any matches it finds.它会将“重复”放在它找到的任何匹配项的第二个版本中。

Then sort on column C to bring "Duplicate" to the top and delete all those rows.然后在 C 列上排序以将“重复”带到顶部并删除所有这些行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果单元格值等于“”,如何编写 Excel 宏以删除整行 - How to Write Excel Macro to Delete Entire Row if cell Value Equals “” VBA excel宏,该宏会迭代一个表并选择所有行,其中A列的行单元格等于 - VBA excel macro that iterates a table and select all rows which row cell at column A equals something VBA-如果列A中的单元格不为空白,则列B等于 - VBA - If a cell in column A is not blank the column B equals Excel Vba宏根据单元格值和列名称删除行或列 - Excel Vba Macro to Delete Row or Column Based on Cell Value and Column Name 如果A列中的单元格不是空白,则B列等于C列等于 - If a cell in column A is not blank then column B equals and column C equals Excel宏查找列B中的最后一行,选择另一个单元格A1,将公式从A1拖动到B中的最后一行 - Excel Macro Find Last Row In Column B, Select another cell A1, drag formula from A1 to last row in B 如果特定列中的单元格有数据,如何告诉 Excel 宏删除整行? - How do I tell an excel macro to delete the entire row if cell in a specific column have data? Excel 宏/VBA - 如果一列中的单元格为空,如何删除整行? - Excel Macro / VBA - How do I delete an entire row if a cell in one column is blank? Excel VBA-如果B列中的单元格包含一个值,则A列等于“值”,但不要覆盖现有的A列数据 - Excel VBA - If a cell in column B contains a value then column A equals “value” BUT do not overwrite existing column A data 基于单元格内容的excel vba宏删除列 - excel vba macro delete column based on cell content
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM