II在Excel中有六个列表,我想根据每个列表的第一项使用VBA来匹配它们

所以我有一个工作表,有6列数据,每列有6列数据。 在六个数据集的每个数据集中,我只想拿出那些具有匹配集合的数字。 例如,

LIST 1 LIST 2 LIST 3 LIST 4 LIST 5 LIST 6 001 ------ 003 ------ 002 ------ 003 ------ 003 ------ 003 ------ 002 ------ 004 ------ 003 ------ 006 ------ 004 ------ 005 ------ 003 ------ 005 ------ 006 ------ 007 ------ 009 ------ 013 ------ 

这是六个数据清单。 只有第一列(001,004等)在每个集合在这个sortingmacros是感兴趣的。 这里,每个列表共享一行“003 —–”。 我想写一个macros,将删除任何不符合其他的行。 有没有一个macros,可以通过这个sorting,离开我只有003 —–?

我一直在写一个循环macros,它说“如果Rng(A1)> Rng.Offset(,6)AND Rng> Rng.Offset(,12)…然后(删除相关行)

然而,为此,我需要涵盖所有可能的可能性。 还有另一个更明显的方法,我错过了?

部分数据截图(点击图片放大)

在这里输入图像说明

这是我想匹配以“BC …”开头的数字,同时保留了每一列的四列

这是代码。 对不起,O(n ^ 4)的复杂性,但它的工作原理。 你的屏幕有什么问题,我看到你需要第6列,所以我把这个常量来帮助你。

 Const STEP_COLUMN As Integer = 6 Sub foo() Dim lastRow As Integer, lastColumn As Integer Dim rowIndex As Integer, columnIndex As Integer Dim ok As Boolean, value As Double lastRow = Range("A10000").End(xlUp).Row lastColumn = Range("XX1").End(xlToLeft).Column For columnIndex = 1 To lastColumn Step STEP_COLUMN For rowIndex = 1 To lastRow value = Cells(rowIndex, columnIndex) Call deleteIfNotExistInEveryColumn(value, lastRow, lastColumn) Next rowIndex Next columnIndex End Sub Sub deleteIfNotExistInEveryColumn(value, lastRow, lastColumn) Dim rowIndex As Integer, columnIndex As Integer For columnIndex = 1 To lastColumn Step STEP_COLUMN hasValue = False For rowIndex = 1 To lastRow If value = Cells(rowIndex, columnIndex) Then hasValue = True End If Next rowIndex If Not hasValue Then ActiveSheet.Cells.Replace What:=value, Replacement:="", LookAt:=xlPart, _ SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False End If Next columnIndex End Sub 

PS:我可以使用匹配和删除不在第一列的每个数字[因为如果该值不在第一列,那么值不是在每一列:-)],但我更喜欢build立完整的代码没有优化