Marking first instance of unique value in large range, on low powered machine

e4xit

New Member
Joined
Nov 1, 2011
Messages
4
Hello, I have a large range of (sales) data which includes multiple orders ("order number"-s). Each order can contain 1 or multiple "lines". Each line can (confusingly) contain more than one entry. The reason for this is that a Line might contain values for: Manufacturing, Delivery, Handling Charge etc.

The result of this is that "Count"-ing order numbers produces incorrect number of orders per customer (in Pivot Table). The same with "Count" of Line Number. What I need to be able to do is to "Count Unique" values, something I am aware that might be possible in Excel 365 Pivot Tables, but not to me.

I have previously CONCATENATED some values to produce an "ID" which will be the same for each customer order (customer number + order number) and for each customer "line" (customer number + order number + line number). The calculation of these "ID"s was ok on my machine.

I then tried to use a formula:
Code:
=(COUNTIF($BA$2:$BA2,$BA2)=1)+0

...which will count the first instance of each ID in the array and return "1" in the adjacent column, of the row which that first instance occurs. Excel then has no difficulty summing these 1's to give me correct number of orders and lines for each customer.

My difficulty has come because when used on final data range it kills my machine. My test range of 100 rows was fine, my actual range of 22,000 rows is painful and I have not even attempted on 65,000 full range. I believe that this is a combination of having to perform
Code:
CONCATENATE
and
Code:
=(COUNTIF($BA$2:$BA2,$BA2)=1)+0
concurrently, where the second formula will become exponentionally slower with increasing range size...

My question is therefore, what woudl be the best way to go about this? Should I be using formulas at all? My intuition says that VBA might be better, alas I do not know any myself (although I am quite a proficient hacker of code I find online). I hope somebody might be able to point me in the right direction here, or supply some code which I can test out to pull this off? The 22,000 row range took almost an hour, so I know my code works, I think it is just horribly inefficient.

My specs are:
OS: Windows 7 Sp1
MS Office: 2010

Any and all help much appreciated.

Sample sheet below. Last two columns are my concatenated ID's which I would like to scan for first unique value, where detection of first instance of unique value would enter a "1" into new columns "Q" and "R" respectively.

Sheet1

*ABCDEFGHIJKLMNOP
1SALNUMSALNVNSALINNSALRDNSALDVNSALDYYSALDMMSALDDDSALEGCSALNYYSALNMMSALNDDSALXTDSALXTTINVIDLINID
2 * *744210261915256582374382015415001201541420151 * *7442201541452565821
3 * *744210261915256582374382015415001201541420151 * *7442201541452565821
4 * *74041026201 *36989374442015416001201541520151 * *74042015415 *369891
5 * *74041026211 *37065374452015416001201541520151 * *74042015415 *370651
6 * *73321026221 *37466374692015423001201542220151 * *73322015422 *374661
7 * *73321026221 *37466374692015423001201542220151 * *73322015422 *374661
8 * *73321026231 492823374892015428001201542720151 * *73322015427 4928231
9 * *73321026241 *3749937502201551001201543020151 * *73322015430 *374991
10 * *73321026241 *3749937502201551001201543020151 * *73322015430 *374991
11 * *72821026251 *37041375482015519001201551820152 * *72822015518 *370411
12 * *72821026251 *37041375482015519001201551820152 * *72822015518 *370411
13 * *72821026261 *37041375492015519001201551820152 * *72822015518 *370411
14 * *72821026261 *37041375492015519001201551820152 * *72822015518 *370411

<tbody>
</tbody>


Excel tables to the web >> Excel Jeanie HTML 4
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
You might try the MATCH function.
Note that the second MATCH refers to 1 row above.
Code:
=IF(MATCH(O2,$O$1:$O$14,0)=MATCH([COLOR="#FF0000"]O1[/COLOR],$O$1:$O$14,0),"",1)
There are also ways to use the FREQUENCY function to just get a count of unique items.
Excel Workbook
KLMNOPQR
1SALNMMSALNDDSALXTDSALXTTINVIDLINID
241420151* *744220154145256582111
341420151* *7442201541452565821
441520151* *74042015415*36989111
541520151* *74042015415*3706511
642220151* *73322015422*37466111
742220151* *73322015422*374661
842720151* *73322015427492823111
943020151* *73322015430*37499111
1043020151* *73322015430*374991
1151820152* *72822015518*37041111
1251820152* *72822015518*370411
1351820152* *72822015518*370411
1451820152* *72822015518*370411
Sheet
 
Upvote 0
There could be an issue with the formula I posted above. If your invoice numbers and customer are grouped together like you have in your example the formula should work and could be changed to leave out the MATCh, however if they are not grouped you will get a bogus answer. Example if an invoice number is in rows 5,6,7, and then again on row 200 the one on row 200 will count as another invoice.
If that is not an issue then the formulas could be changed to:
Code:
=IF(O2=O1,"",1)
=IF(P2=P1,"",1)

If you want a count of unique invoices by customer them maybe the FREQUENCY formula in the example below.
NOTE - This is an array formula and must be entered with CTRL-SHIFT-ENTER (command-return on MAC)

Copy formula down and change ranges to match your data.
Excel Workbook
KLMNOPQRST
1SALNMMSALNDDSALXTDSALXTTINVIDLINIDLINIDCount
241420151* *7442201541452565821525658211
341420151* *7442201541452565821*3698911
441520151* *74042015415*369891*3706511
541520151* *74042015415*370651*3746611
642220151* *73322015422*37466149282311
742220151* *73322015422*374661*3749911
842720151* *733220154274928231*3704112
943020151* *73322015430*374991
1043020151* *73322015430*374991
1151820152* *72822015518*370411
1251820152* *72822015518*370411
1351820152* *72822015518*370411
1451820152* *72822015518*370411
15* *74042015415*370411
Sheet
 
Upvote 0
Hi, thanks AhoyNC for your help. I think that having tried to hack your long (final) formula and failed, that the easiest way is for me to manually sort the incoming data by InvoiceID (after refresh) and use the somewhat simpler and faster
Code:
=IF(O2=O1,"",1)
formulas.

Theoretically there is a small possibility that invoice numbers could not be in adjacent rows as they come in (although in theory it should not happen), but I think this method will prove accurate enough for my needs right now. So, thanks again for your help.

Just one question though, the "back end" of the FREQUENCY formula seems to be able to "count and remember" if a value has appeared before in a list, could this same process be hijacked either through VBA or standard formula to do the same "count unique", but also just to put a 1 in adjacent cell for each first instance? This seems like by far the closest natural function operation to what I need, but just needs a small tweak.
 
Upvote 0
Here is a formula based on one from Aladin Akyurek that I think will do what you want.
Change ranges to match your data and copy down rows.
This is an array formula and must be entered with
Excel Workbook
KLMNOPQR
1SALNMMSALNDDSALXTDSALXTTINVIDLINID
241420151* *744220154145256582111
341420151* *7442201541452565821
441520151* *74042015415*36989111
541520151* *74042015415*3706511
642220151* *73322015422*37466111
742220151* *73322015422*374661
842720151* *73322015427492823111
943020151* *73322015430*37499111
1043020151* *73322015430*374991
1151820152* *72822015518*37041111
1251820152* *72822015518*370411
1351820152* *72822015518*370411
1451820152* *72822015518*370411
15* *74042015415*369891
CTRL-SHIFT-ENTER.
 
Upvote 0

Forum statistics

Threads
1,215,917
Messages
6,127,703
Members
449,399
Latest member
VEVE4014

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top