从主表中删除不一致的实例

香港专业教育学院有3张工作表的Excel工作簿。 这些表都包含名称列表,所有列表在某些方面重叠,因为它们来自不同的来源。

我试图使工作表1一个独特的名单,即。 这样它将只包含在表单2和表单3中不存在的名称。

我指出了使用数据>删除重复的方向,但这并没有跨表,有没有办法可以使用vbamacros?

您可以在工作Sheet 1中的表格一侧使用VLOOKUP放在表2和表3中,如果发现匹配,则返回1,否则返回1。
然后,一个Excel例程将只扫描这两个列的1 – 如果它发现一个,那么它删除行。

下面是我正在谈论的公式的一个例子:

在这里输入图像说明

假设column D将只是BC的总和。

macros可以在列D中寻找任何>0

不是最好的做法,从一个循环内对一个集合进行操作,但像下面这样的循环往下循环删除行:

 Option Explicit Sub deleteRows() Dim i As Integer i = 2 Do If Cells(i, 4) > 0 Then Cells(i, 4).EntireRow.Delete xlUp Else i = i + 1 End If Loop Until IsEmpty(Cells(i, 4)) = True End Sub 

另一种方法是将sheet 2sheet 3所有名称加载到array 。 然后回到sheet 1并为每个名称运行通过数组testing,如果它等于任何值,如果它删除整行。 所以要使用数组就像下面这样: 这假设每个列表是在column A并开始在row 2

 Sub Macro1() Dim names() As String Dim i As Integer i = 1 'add names from sheet 2 into the array Do ReDim Preserve names(i) names(i) = ThisWorkbook.Worksheets("Sheet2").Cells(i + 1, 1) i = i + 1 Loop Until IsEmpty(ThisWorkbook.Worksheets("Sheet2").Cells(i + 1, 1)) = True 'add names from sheet 3 into the array Do ReDim Preserve names(i) names(i) = ThisWorkbook.Worksheets("Sheet3").Cells(i + 1, 1) i = i + 1 Loop Until IsEmpty(ThisWorkbook.Worksheets("Sheet3").Cells(i + 1, 1)) = True 'use the names array to test each row in sheet 1 Dim j As Integer j = 2 Do Dim deleteOccured As Boolean deleteOccured = False Dim x For Each x In names If x = Cells(j, 1) Then Cells(j, 1).EntireRow.Delete xlUp deleteOccured = True End If Next x If deleteOccured = False Then j = j + 1 End If deleteOccured = False Loop Until IsEmpty(Cells(j, 1)) = True End Sub 

警告 我需要强调的是,这些循环并不完美:任何编码的一般性最佳实践是,您不应该在循环中对同一数组执行操作时循环数组…….我希望有人将帮助我与此。