Script to subtract quantities in two sheets with multiple conditions

BrutalDawg

New Member
Joined
Jun 10, 2015
Messages
41
I have spent the last few hours trying to find something similar to the issue I am having and currently have smoke coming out of my ears!
I have two sheets, one with orders the second with sales.
I need help automatically subtracting from orders based on sales.
I am trying to develop a script that would run through every sale and if location and item are exactly the same, subtract quantity sold from the quantity ordered to give a "new due" quantity.
There will be sales with no order which is a non issue, but would be nice if those results could go to a different tab. Below is a summary of what is required in n00b terms.
<code style="margin: 0px; padding: 1px 5px; border: 0px; font-size: 13px; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; background-color: rgb(239, 240, 241); white-space: pre-wrap;">Sheet1</code> is sold, <code style="margin: 0px; padding: 1px 5px; border: 0px; font-size: 13px; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; background-color: rgb(239, 240, 241); white-space: pre-wrap;">Sheet2</code> is order.
If <code style="margin: 0px; padding: 1px 5px; border: 0px; font-size: 13px; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; background-color: rgb(239, 240, 241); white-space: pre-wrap;">Sheet1</code> C2 & F2 are same as any row in <code style="margin: 0px; padding: 1px 5px; border: 0px; font-size: 13px; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; background-color: rgb(239, 240, 241); white-space: pre-wrap;">Sheet2</code> (A2,D2 are correlating fields) subtract <code style="margin: 0px; padding: 1px 5px; border: 0px; font-size: 13px; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; background-color: rgb(239, 240, 241); white-space: pre-wrap;">Sheet1</code>G2, from <code style="margin: 0px; padding: 1px 5px; border: 0px; font-size: 13px; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; background-color: rgb(239, 240, 241); white-space: pre-wrap;">Sheet2</code> F2.

ABCDEFG
TypeDateShip To Address 1Ship To Address 2Ship ZipItemQty
InvoiceOhio
InvoiceOklahoma
InvoiceTEXAS
InvoiceTEXAS
InvoiceFlorida
InvoiceOklahoma
InvoiceMexico
InvoiceOhio

<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
[TD="align: center"]1[/TD]

[TD="align: center"]2[/TD]

[TD="align: right"]1/25/2017[/TD]

[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"]5698[/TD]
[TD="align: right"]60[/TD]

[TD="align: center"]3[/TD]

[TD="align: right"]1/25/2017[/TD]

[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"]5693[/TD]
[TD="align: right"]50[/TD]

[TD="align: center"]4[/TD]

[TD="align: right"]1/24/2017[/TD]

[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"]5701[/TD]
[TD="align: right"]220[/TD]

[TD="align: center"]5[/TD]

[TD="align: right"]1/25/2017[/TD]

[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"]5699[/TD]
[TD="align: right"]112[/TD]

[TD="align: center"]6[/TD]

[TD="align: right"]1/26/2017[/TD]

[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"]5729[/TD]
[TD="align: right"]20[/TD]

[TD="align: center"]7[/TD]

[TD="align: right"]1/25/2017[/TD]

[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"]5693[/TD]
[TD="align: right"]205[/TD]

[TD="align: center"]8[/TD]

[TD="align: right"]1/25/2017[/TD]

[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"]5695[/TD]
[TD="align: right"]165[/TD]

[TD="align: center"]9[/TD]

[TD="align: right"]1/25/2017[/TD]

[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"]5698[/TD]
[TD="align: right"]45[/TD]

</tbody>
Sold



ABCDEFG
PlantIssueDatePurchaseOrderNumberPartNumberTimingQuantityWhen
TexasAI437BD1843Immediate:Discrete
OklahomaAI589AD1852Firm:Weekly Bucket (Monday through Sunday)
TexasAI437BD1876Firm:Weekly Bucket (Monday through Sunday)
TexasAI437BD1843Firm:Weekly Bucket (Monday through Sunday)
OklahomaAI589AD1882Firm:Weekly Bucket (Monday through Sunday)
OklahomaAI589AD1852Firm:Weekly Bucket (Monday through Sunday)
TexasAI437BD1876Firm:Weekly Bucket (Monday through Sunday)
TexasAI437BD1843Firm:Weekly Bucket (Monday through Sunday)
OHIOAI589AD1874Firm:Weekly Bucket (Monday through Sunday)
OHIOAI589AD1852Firm:Weekly Bucket (Monday through Sunday)
MexicoAI437BD1876Firm:Weekly Bucket (Monday through Sunday)
MexicoAI589AD1852Firm:Weekly Bucket (Monday through Sunday)
OklahomaAI589AD1882Firm:Weekly Bucket (Monday through Sunday)
OklahomaAI589AD1852Firm:Weekly Bucket (Monday through Sunday)
TexasAI437BD1876Firm:Weekly Bucket (Monday through Sunday)
TexasAI437BD1843Firm:Weekly Bucket (Monday through Sunday)
OHIOAI589AD1874Firm:Weekly Bucket (Monday through Sunday)
OHIOAI589AD1852Firm:Weekly Bucket (Monday through Sunday)
MexicoAI437BD1876Firm:Weekly Bucket (Monday through Sunday)
MexicoAI589AD1852Firm:Weekly Bucket (Monday through Sunday)
OklahomaAI589AD1882Firm:Weekly Bucket (Monday through Sunday)
OklahomaAI589AD1852Firm:Weekly Bucket (Monday through Sunday)
TexasAI437BD1876Firm:Weekly Bucket (Monday through Sunday)

<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
[TD="align: center"]1[/TD]

[TD="align: center"]2[/TD]

[TD="align: right"]1/22/2017[/TD]

[TD="align: right"]13[/TD]
[TD="align: right"]1/22/2017[/TD]

[TD="align: center"]3[/TD]

[TD="align: right"]1/22/2017[/TD]

[TD="align: right"]28[/TD]
[TD="align: right"]1/23/2017[/TD]

[TD="align: center"]4[/TD]

[TD="align: right"]1/22/2017[/TD]

[TD="align: right"]9[/TD]
[TD="align: right"]1/23/2017[/TD]

[TD="align: center"]5[/TD]

[TD="align: right"]1/22/2017[/TD]

[TD="align: right"]174[/TD]
[TD="align: right"]1/23/2017[/TD]

[TD="align: center"]6[/TD]

[TD="align: right"]1/22/2017[/TD]

[TD="align: right"]3[/TD]
[TD="align: right"]1/30/2017[/TD]

[TD="align: center"]7[/TD]

[TD="align: right"]1/22/2017[/TD]

[TD="align: right"]205[/TD]
[TD="align: right"]1/30/2017[/TD]

[TD="align: center"]8[/TD]

[TD="align: right"]1/22/2017[/TD]

[TD="align: right"]3[/TD]
[TD="align: right"]1/30/2017[/TD]

[TD="align: center"]9[/TD]

[TD="align: right"]1/22/2017[/TD]

[TD="align: right"]110[/TD]
[TD="align: right"]1/30/2017[/TD]

[TD="align: center"]10[/TD]

[TD="align: right"]1/22/2017[/TD]

[TD="align: right"]60[/TD]
[TD="align: right"]1/30/2017[/TD]

[TD="align: center"]11[/TD]

[TD="align: right"]1/22/2017[/TD]

[TD="align: right"]42[/TD]
[TD="align: right"]1/30/2017[/TD]

[TD="align: center"]12[/TD]

[TD="align: right"]1/22/2017[/TD]

[TD="align: right"]1[/TD]
[TD="align: right"]1/30/2017[/TD]

[TD="align: center"]13[/TD]

[TD="align: right"]1/22/2017[/TD]

[TD="align: right"]164[/TD]
[TD="align: right"]1/30/2017[/TD]

[TD="align: center"]14[/TD]

[TD="align: right"]1/22/2017[/TD]

[TD="align: right"]2[/TD]
[TD="align: right"]2/6/2017[/TD]

[TD="align: center"]15[/TD]

[TD="align: right"]1/22/2017[/TD]

[TD="align: right"]204[/TD]
[TD="align: right"]2/6/2017[/TD]

[TD="align: center"]16[/TD]

[TD="align: right"]1/22/2017[/TD]

[TD="align: right"]6[/TD]
[TD="align: right"]2/6/2017[/TD]

[TD="align: center"]17[/TD]

[TD="align: right"]1/22/2017[/TD]

[TD="align: right"]188[/TD]
[TD="align: right"]2/6/2017[/TD]

[TD="align: center"]18[/TD]

[TD="align: right"]1/22/2017[/TD]

[TD="align: right"]60[/TD]
[TD="align: right"]2/6/2017[/TD]

[TD="align: center"]19[/TD]

[TD="align: right"]1/22/2017[/TD]

[TD="align: right"]299[/TD]
[TD="align: right"]2/6/2017[/TD]

[TD="align: center"]20[/TD]

[TD="align: right"]1/22/2017[/TD]

[TD="align: right"]3[/TD]
[TD="align: right"]2/6/2017[/TD]

[TD="align: center"]21[/TD]

[TD="align: right"]1/22/2017[/TD]

[TD="align: right"]197[/TD]
[TD="align: right"]2/6/2017[/TD]

[TD="align: center"]22[/TD]

[TD="align: right"]1/22/2017[/TD]

[TD="align: right"]10[/TD]
[TD="align: right"]2/13/2017[/TD]

[TD="align: center"]23[/TD]

[TD="align: right"]1/22/2017[/TD]

[TD="align: right"]166[/TD]
[TD="align: right"]2/13/2017[/TD]

[TD="align: center"]24[/TD]

[TD="align: right"]1/22/2017[/TD]

[TD="align: right"]4[/TD]
[TD="align: right"]2/13/2017[/TD]

</tbody>
Order



ABCDEFGH
PlantIssueDatePurchaseOrderNumberPartNumberTimingQuantityWhen
TEXASD1876Firm:Weekly Bucket (Monday through Sunday)
TEXASD1843Firm:Weekly Bucket (Monday through Sunday)
TEXASD1876Firm:Weekly Bucket (Monday through Sunday)
TEXASD1843Firm:Weekly Bucket (Monday through Sunday)
OHIOD1852Firm:Weekly Bucket (Monday through Sunday)
MexicoD1876Firm:Weekly Bucket (Monday through Sunday)
MexicoD1852Firm:Weekly Bucket (Monday through Sunday)
OklahomaD1882Firm:Weekly Bucket (Monday through Sunday)
OklahomaD1852Firm:Weekly Bucket (Monday through Sunday)
TEXASD1876Firm:Weekly Bucket (Monday through Sunday)
TEXASD1843Firm:Weekly Bucket (Monday through Sunday)
OHIOD1874Firm:Weekly Bucket (Monday through Sunday)
OHIOD1852Firm:Weekly Bucket (Monday through Sunday)
MexicoD1876Firm:Weekly Bucket (Monday through Sunday)
OklahomaD1882Firm:Weekly Bucket (Monday through Sunday)(-37)
OklahomaD1852Firm:Weekly Bucket (Monday through Sunday)
TEXASD1876Firm:Weekly Bucket (Monday through Sunday)
TEXASD1882Firm:Weekly Bucket (Monday through Sunday)
WOULD BE IDEAL RESULT FOR (F)

<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
[TD="align: center"]1[/TD]

[TD="align: right"][/TD]

[TD="align: center"]2[/TD]
[TD="bgcolor: #FFFF00"]TEXAS[/TD]
[TD="bgcolor: #FFFF00, align: right"]1/22/2017[/TD]
[TD="bgcolor: #FFFF00, align: right"][/TD]
[TD="bgcolor: #FFFF00"]D1843[/TD]
[TD="bgcolor: #FFFF00"]Immediate:Discrete[/TD]
[TD="bgcolor: #C6EFCE, align: right"]-299[/TD]
[TD="bgcolor: #FFFF00, align: right"]1/22/2017[/TD]
[TD="align: right"]0[/TD]

[TD="align: center"]3[/TD]
[TD="bgcolor: #FFFF00"]Oklahoma[/TD]
[TD="bgcolor: #FFFF00, align: right"]1/22/2017[/TD]
[TD="bgcolor: #FFFF00, align: right"][/TD]
[TD="bgcolor: #FFFF00"]D1852[/TD]
[TD="bgcolor: #FFFF00"]Firm:Weekly Bucket (Monday through Sunday)[/TD]
[TD="bgcolor: #C6EFCE, align: right"]-177[/TD]
[TD="bgcolor: #FFFF00, align: right"]1/23/2017[/TD]
[TD="align: right"]0[/TD]

[TD="align: center"]4[/TD]

[TD="align: right"]1/22/2017[/TD]
[TD="align: right"][/TD]

[TD="align: right"]9[/TD]
[TD="align: right"]1/23/2017[/TD]
[TD="align: right"][/TD]

[TD="align: center"]5[/TD]

[TD="align: right"]1/22/2017[/TD]
[TD="align: right"][/TD]

[TD="align: right"]174[/TD]
[TD="align: right"]1/23/2017[/TD]
[TD="align: right"]-45[/TD]

[TD="align: center"]6[/TD]
[TD="bgcolor: #FFFF00"]Oklahoma[/TD]
[TD="bgcolor: #FFFF00, align: right"]1/22/2017[/TD]
[TD="bgcolor: #FFFF00, align: right"][/TD]
[TD="bgcolor: #FFFF00"]D1882[/TD]
[TD="bgcolor: #FFFF00"]Firm:Weekly Bucket (Monday through Sunday)[/TD]
[TD="bgcolor: #C6EFCE, align: right"]-47[/TD]
[TD="bgcolor: #FFFF00, align: right"]1/30/2017[/TD]
[TD="align: right"]0[/TD]

[TD="align: center"]7[/TD]
[TD="bgcolor: #FFFF00"]Oklahoma[/TD]
[TD="bgcolor: #FFFF00, align: right"]1/22/2017[/TD]
[TD="bgcolor: #FFFF00, align: right"][/TD]
[TD="bgcolor: #FFFF00"]D1852[/TD]
[TD="bgcolor: #FFFF00"]Firm:Weekly Bucket (Monday through Sunday)[/TD]
[TD="bgcolor: #FFFF00, align: right"]205[/TD]
[TD="bgcolor: #FFFF00, align: right"]1/30/2017[/TD]
[TD="bgcolor: #FFC7CE, align: right"]28[/TD]

[TD="align: center"]8[/TD]

[TD="align: right"]1/22/2017[/TD]
[TD="align: right"][/TD]

[TD="align: right"]3[/TD]
[TD="align: right"]1/30/2017[/TD]
[TD="align: right"][/TD]

[TD="align: center"]9[/TD]

[TD="align: right"]1/22/2017[/TD]
[TD="align: right"][/TD]

[TD="align: right"]110[/TD]
[TD="align: right"]1/30/2017[/TD]
[TD="align: right"][/TD]

[TD="align: center"]10[/TD]
[TD="bgcolor: #FFFF00"]OHIO[/TD]
[TD="bgcolor: #FFFF00, align: right"]1/22/2017[/TD]
[TD="bgcolor: #FFFF00, align: right"][/TD]
[TD="bgcolor: #FFFF00"]D1874[/TD]
[TD="bgcolor: #FFFF00"]Firm:Weekly Bucket (Monday through Sunday)[/TD]
[TD="bgcolor: #C6EFCE, align: right"]0[/TD]
[TD="bgcolor: #FFFF00, align: right"]1/30/2017[/TD]
[TD="align: right"][/TD]

[TD="align: center"]11[/TD]

[TD="align: right"]1/22/2017[/TD]
[TD="align: right"][/TD]

[TD="align: right"]-3[/TD]
[TD="align: right"]1/30/2017[/TD]
[TD="align: right"]0[/TD]

[TD="align: center"]12[/TD]

[TD="align: right"]1/22/2017[/TD]
[TD="align: right"][/TD]

[TD="align: right"]1[/TD]
[TD="align: right"]1/30/2017[/TD]
[TD="align: right"][/TD]

[TD="align: center"]13[/TD]

[TD="align: right"]1/22/2017[/TD]
[TD="align: right"][/TD]

[TD="align: right"]164[/TD]
[TD="align: right"]1/30/2017[/TD]
[TD="align: right"][/TD]

[TD="align: center"]14[/TD]

[TD="align: right"]1/22/2017[/TD]
[TD="align: right"][/TD]

[TD="align: right"]2[/TD]
[TD="align: right"]2/6/2017[/TD]
[TD="align: right"][/TD]

[TD="align: center"]15[/TD]

[TD="align: right"]1/22/2017[/TD]
[TD="align: right"][/TD]

[TD="align: right"]204[/TD]
[TD="align: right"]2/6/2017[/TD]
[TD="align: right"][/TD]

[TD="align: center"]16[/TD]

[TD="align: right"]1/22/2017[/TD]
[TD="align: right"][/TD]

[TD="align: right"]6[/TD]
[TD="align: right"]2/6/2017[/TD]
[TD="align: right"][/TD]

[TD="align: center"]17[/TD]

[TD="align: right"]1/22/2017[/TD]
[TD="align: right"][/TD]

[TD="align: right"]188[/TD]
[TD="align: right"]2/6/2017[/TD]
[TD="align: right"][/TD]

[TD="align: center"]18[/TD]

[TD="align: right"]1/22/2017[/TD]
[TD="align: right"][/TD]

[TD="align: right"]60[/TD]
[TD="align: right"]2/6/2017[/TD]
[TD="align: right"][/TD]

[TD="align: center"]19[/TD]

[TD="align: right"]1/22/2017[/TD]
[TD="align: right"][/TD]

[TD="align: right"]299[/TD]
[TD="align: right"]2/6/2017[/TD]
[TD="align: right"]296[/TD]

[TD="align: center"]20[/TD]

[TD="align: right"]1/22/2017[/TD]
[TD="align: right"][/TD]

[TD="align: right"]3[/TD]
[TD="align: right"]2/6/2017[/TD]
[TD="align: right"][/TD]

[TD="align: center"]21[/TD]
[TD="bgcolor: #FFFF00"]Mexico[/TD]
[TD="bgcolor: #FFFF00, align: right"]1/22/2017[/TD]
[TD="bgcolor: #FFFF00, align: right"][/TD]
[TD="bgcolor: #FFFF00"]D1852[/TD]
[TD="bgcolor: #FFFF00"]Firm:Weekly Bucket (Monday through Sunday)[/TD]
[TD="bgcolor: #FFC7CE, align: right"]32[/TD]
[TD="bgcolor: #FFFF00, align: right"]2/6/2017[/TD]
[TD="align: right"][/TD]

[TD="align: center"]22[/TD]

[TD="align: right"]1/22/2017[/TD]
[TD="align: right"][/TD]

[TD="align: right"]10[/TD]
[TD="align: right"]2/13/2017[/TD]

[TD="align: center"]23[/TD]

[TD="align: right"]1/22/2017[/TD]
[TD="align: right"][/TD]

[TD="align: right"]166[/TD]
[TD="align: right"]2/13/2017[/TD]
[TD="align: right"][/TD]

[TD="align: center"]24[/TD]

[TD="align: right"]1/22/2017[/TD]
[TD="align: right"][/TD]

[TD="align: right"]4[/TD]
[TD="align: right"]2/13/2017[/TD]
[TD="align: right"][/TD]

[TD="align: center"]25[/TD]

[TD="align: right"]1/22/2017[/TD]
[TD="align: right"][/TD]

[TD="align: right"]110[/TD]
[TD="align: right"]2/20/2017[/TD]
[TD="align: right"]65[/TD]

[TD="align: center"]26[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]

</tbody>
Results




On the results tab everything highlighted in yellow is what would have changed. Items not highlighted in yellow there was no sale. The green and red formatting was for reference and is not required if to difficult. (would be ideal) column H, is illustrating, if this is even possible, once the order quantity reaches 0, for the script to keep searching sheet for next order with same criteria and subtract remain quantity. Anything will help, I am currently doing this manually over the weekend and generally 150+ sales per week and is very time consuming.

In simple terms, I am trying to have a script go through each sale row and subtract quantity from the order sheet if the plant and part number are the same. I will sort the order sheet based on when they are do so it removes from correct order.

Thanks for any help!

 

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.

Forum statistics

Threads
1,223,228
Messages
6,170,876
Members
452,363
Latest member
merico17

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