VBA Excel - Sort a daily delivery manifest into groups based on complex rules - excel

I am trying to come up with a way to sort my Excel-Table. I wrote a very basic VBA to do this when it was a random sort only. Now there's complex parameters/rules I have to meet and it's way outside my skillset.
The problem: I receive a daily file with a list of items via shipments like the example below. The list can have as few as 1 and as many as 24 items. I have to sort these by restaurant.
Example Original List
ITEM SHIPMENT
Oranges 1
Apples 1
Grapes 1
Pears 2
Pork 3
Chicken 4
Rice 5
Peas 5
Beans 5
Water 5
Corn 5
Milk 5
Eggs 5
Salmon 6
Tofu 7
Juice 8
Cheese 8
Salt 8
Pepper 9
Onions 10
Oats 11
Barley 11
Kale 11
Chips 12
The items need to be sorted out to 6 restaurants and there are complex rules:
Overall Rules
No Restaurant can have more than 1 item from a shipment
No Restaurant can have more than 4 items
Sorting Rules
Restaurant 1 always gets the first two items (Items 1-2)
Restaurant 2-5 evenly gets the next 16 items (Items 3-18)
Restaurant 6 gets the next 2 items (Items 19-20)
Restaurant 1 and 6 then evenly get the last 4 items (Items 21-24)
If there are more than 6 items in a shipment (more items than restaurants) the extra items stay in the warehouse.
The Overall Rules override the sorting rules. For example in our example list Restaurant 1 cannot have both Oranges and Apples since they are from the same shipment so the sort changes.
Example Sort
Restaurant 1 Shipment
Oranges 1
Pears 2
Rice 5
Kale 11
Restaurant 2
Apples 1
Peas 5
Salmon 6
Salt 8
Restaurant 3
Grapes 1
Beans 5
Tofu 7
Pepper 9
Restaurant 4
Pork 3
Water 5
Juice 8
Onions 10
Restaurant 5
Chicken 4
Corn 5
Cheese 8
Oats 11
Restaurant 6
Milk 5
Barley 11
Chips 12
Warehouse Items
Eggs 5
Looking at it as a whole now I'm not even sure this is possible and I have no idea how to go about doing it. If anyone has any input I'd love to hear it. Thank you so much for your help.

Related

On SQL with one-to-many merging and many as a narrowing condition

Use sqlalchemy
Parent table
id name
1 sea bass
2 Tanaka
3 Mike
4 Louis
5 Jack
Child table
id user_id pname number
1 1 Apples 2
2 1 Banana 1
3 1 Grapes 3
4 2 Apples 2
5 2 Banana 2
6 2 Grapes 1
7 3 Strawberry 5
8 3 Banana 3
9 3 Grapes 1
I want to sort by parent id with apples and number of bananas, but when I search for "parent id with apples", the search is filtered and the bananas disappear. I have searched for a way to achieve this, but have not been able to find it.
Thank you in advance for your help.
Translated with www.DeepL.com/Translator (free version)

SUMIFS excluding two criteria

I'm trying to do SUMIFS with two criteria I want to exclude.
I want to count the amount of fruit sold by all, excluding cherries sold by James.
A
B
C
Qty
Product
Salesperson
5
Apples
James
10
Apples
Jack
15
Apples
Ben
20
Bananas
Ben
15
Bananas
Jack
10
Cherries
James
5
Grapes
Ben
10
Grapes
James
15
Cherries
Jack
20
Melons
Ben
I've tried
=SUMIFS(A:A,B:B,AND(B:B"<>Cherries",C:C"<> James"))
but got an error.
=SUMIFS(A:A,B:B,"<>Cherries",C:C,"<>James")
Also does not work as it doesn't count and Cherries or anything sold by James.
From the data above I would expect 115 as my answer.
subtract the part from the whole:
=SUM(A:A)-SUMIFS(A:A,B:B,"Cherries",C:C,"James")

Combining multiple values attached to the same variable in Excel

I need to add multiple values related to the same variable into one single entry. Here is what my worksheet looks like:
ColA ColB ColC
Onion Ounces 12
Onion Ounces 6
Carrot Kilo 3
Basil Grams 25
Carrot Kilo 6
Basil Cups 3
Here is what I am trying to figure out how to do:
ColA ColB ColC
Onion Ounces 12
Onion Ounces 6
Carrot Kilo 3
Basil Grams 25
Carrot Kilo 6
Basil Cups 3
__________________
Onion Ounces 18
Carrot Kilo 9
Basil Grams 25
Basil Cups 3
*If colA matches (both entries are 'Basil'), but colB doesn't match (one entry is 'ounces' and one is 'cups') it needs to have separate entries.
I'm very new to excel and not even sure how to phrase this question correctly, so my apologies if this has been answered many times before!
Thank you so much for your help!
Use Query:
=QUERY(A1:C6,"Select A,B,Sum(C) group by A,B")

Add rows according to other rows

My DataFrame object similar to this one:
Product StoreFrom StoreTo Date
1 out melon StoreQ StoreP 20170602
2 out cherry StoreW StoreO 20170614
3 out Apple StoreE StoreU 20170802
4 in Apple StoreE StoreU 20170812
I want to avoid duplications, in 3rd and 4th row show same action. I try to reach
Product StoreFrom StoreTo Date Days
1 out melon StoreQ StoreP 20170602
2 out cherry StoreW StoreO 20170614
5 in Apple StoreE StoreU 20170812 10
and I got more than 10k entry. I could not find similar work to this. Any help will be very useful.
d1 = df.assign(Date=pd.to_datetime(df.Date.astype(str)))
d2 = d1.assign(Days=d1.groupby(cols).Date.apply(lambda x: x - x.iloc[0]))
d2.drop_duplicates(cols, 'last')
io Product StoreFrom StoreTo Date Days
1 out melon StoreQ StoreP 2017-06-02 0 days
2 out cherry StoreW StoreO 2017-06-14 0 days
4 in Apple StoreE StoreU 2017-08-12 10 days

Excel PivotTable for counting words for female and male

I have a excel like the following form:
words male words female
I 2 rose 4
am 3 baby 6
sunny 4 slim 9
baby 5 travel 11
football 9
I want this excel to be a new excel like the following form with Excel PivotTable. If there are common words between male and female, the value of each other, if there is no, just show their own value, other are space.
words male female
I 2
am 3
sunny 4
baby 5 6
football 9
rose 4
slim 9
travel 11
Thanks for your time and consideration!
If you paste the data sets on top of each other (cut/paste) and add a label for male/female, a standard pivot table would do the trick:
words count gender
I 2 male
am 3 male
sunny 4 male
baby 5 male
football 9 male
rose 4 female
baby 6 female
slim 9 female
travel 11 female
Pivoted:
Row Labels female male
am 3
baby 6 5
football 9
I 2
rose 4
slim 9
sunny 4
travel 11
If you can't do this, are you open to VBA?

Resources