一列中的值,而另一列中的值

VBA问题:我在Excel中有两列:

AB 10 6 5 1 6 4 2 7 8 9 4 8 9 10 7 5 3 11 

现在我需要结果来包含在列B,但不是列A中的值。我知道,我可以使用CountIf来解决问题。 但是,有什么办法,我可以在VBA没有循环?

尝试这个:

 Sub Demo() Dim ws As Worksheet Dim cel As Range, rngA As Range, rngB As Range Dim lastRowA As Long, lastRowB As Long Application.ScreenUpdating = False Set ws = ThisWorkbook.Sheets("Sheet3") 'change Sheet3 to your data range With ws lastRowA = .Cells(.Rows.Count, "A").End(xlUp).Row 'last row with data in Column A lastRowB = .Cells(.Rows.Count, "B").End(xlUp).Row 'last row with data in Column B Set rngA = .Range("A2:A" & lastRowA) 'range with data in Column A Set rngB = .Range("B2:B" & lastRowB) 'range with data in Column B .Range("C2").Formula = "=IF(COUNTIF(" & rngA.Address & ",$B2)=0,B2,"""")" 'enter formula in Cell C2 .Range("C2").AutoFill Destination:=.Range("C2:C" & lastRowB) 'drag formula down .Range("C2:C" & lastRowB).Value = .Range("C2:C" & lastRowB).Value 'keep only values .Range("C2:C" & lastRowB).SpecialCells(xlCellTypeBlanks).Delete Shift:=xlUp 'delte blank rows End With Application.ScreenUpdating = True End Sub 

在这里输入图像说明

在下面的公式中尝试,在D2中放置下面的公式,selectD2到D11并按CTRL + D。

 =IF(ISNA(MATCH(B2,A:A,0))=TRUE,B2 & " Not Exist in Column A", B2 & " Exist in Column A") 

OP 在这里输入图像说明

只是为了好玩(和速度),你也可以用SQL来做到这一点:

 Sub SqlSelectExample() 'list elements in col C not present in col B Dim con As ADODB.Connection Dim rs As ADODB.Recordset Set con = New ADODB.Connection con.Open "Driver={Microsoft Excel Driver (*.xls)};" & _ "DriverId=790;" & _ "Dbq=" & ThisWorkbook.FullName & ";" & _ "DefaultDir=" & ThisWorkbook.FullName & ";ReadOnly=False;" Set rs = New ADODB.Recordset rs.Open "select ccc.test3 from [Sheet1$] ccc left join [Sheet1$] bbb on ccc.test3 = bbb.test2 where bbb.test2 is null ", _ con, adOpenStatic, adLockOptimistic Range("g10").CopyFromRecordset rs '-> returns values without match rs.MoveLast Debug.Print rs.RecordCount 'get the # records rs.Close Set rs = Nothing Set con = Nothing End Sub 

你将不得不重新做一点SQL。