Hello Juan,
I would not use worksheet functions to calculate this. See here,
please:
http://sulprobil.com/html/count_unique.html
I would suggest to use my UDF Pfreq:
http://sulprobil.com/html/pfreq.html
Use the =Pfreq(Pfreq(A1:A400000,B1:B400000)) approach if its working.
Try my UDF Pstat if your data amount is too large for Pfreq.
http://sulprobil.com/html/pstat.html
Regards,
Bernd