What about doing it with no loops? Here is a one-liner (albeit a long one) UDF (user defined function) that you can use directly in a formula on a worksheet...
HOW TO INSTALL UDFsCode:Function CountDupes(R1 As Range, R2 As Range) As Long CountDupes = Evaluate("SUM(0+ISNUMBER(TRANSPOSE(FIND("", ""&TRIM(MID(SUBSTITUTE(" & _ R1.Address & ","", "",REPT("" "",99)),ROW(A1:A" & UBound(Split(R1.Value, _ ",")) + 1 & ")*99-98,99))&"", "","", ""&" & R2.Address & "&"", ""))))") End Function
------------------------------------
If you are new to UDFs, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. You can now use CountDupes just like it was a built-in Excel function. For example,
="Number of matching numbers = " & CountDupes(A1,B1)
If you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.




Reply With Quote
Bookmarks