Calculate sum and rearrange rows to get value

Cimpcrro

New Member
Joined
Nov 16, 2021
Messages
16
Office Version
  1. 365
Platform
  1. Windows
I have a table with positive and negative values in the same column and I need to order them so their sum at each row is closest to 0 and I need to do this based on article number.
For example, for article X I have the next order:
Book2
CD
1Item no.Value
2111100
3111200
4111-50
5111-70
6111-30
7111-40
8111-50
9111-80
10111-100
Sheet1

I need to rearrange it like this:

Book2
CD
12Item no.Value
13111100
14111-50
15111-70
16111200
17111-30
18111-40
19111-50
20111-80
21111-100
Sheet1


Thank you!
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
I don't see any pattern or logic in the answered data . You need to elaborate more .
As there can be more than one answers .
 
Upvote 0
I don't understand the logic of your example.
Can you walk us through it?
 
Upvote 0
I don't see any pattern or logic in the answered data . You need to elaborate more .
As there can be more than one answers .
It's a simulation on invetory progress.
First value should be the first positive value (eithter original inventory or first incoming quantity), Following values should be in order the negative values (sales orders). If the inventory (sum of above lines) <=0 we need to insert the next positive value (purchase).
Thanks in advance for your help!
 
Upvote 0
I don't understand the logic of your example.
Can you walk us through it?
It's a simulation on invetory progress. First values should be the first positive value (eithter original inventory or first incoming quantity), Following values should be in order the negative values (sales orders). If the inventory (sum of above lines) <=0 we need to insert the next positive value (purchase).
Thanks in advance for your help!
 
Upvote 0
Is your data actually in an Excel Data Table?
I ask because you mentioned "table", but Excel tables typically look something like this (note the formatting):
1637256609740.png


Or maybe you are just using the term table "loosely", and you just mean it is data listed down a column?
That answer may effect how to try craft a solution for you.

Also, the only way I can really think of how to do this is to use VBA.
Is that acceptable?
 
Upvote 0
Is your data actually in an Excel Data Table?
I ask because you mentioned "table", but Excel tables typically look something like this (note the formatting):
View attachment 51591

Or maybe you are just using the term table "loosely", and you just mean it is data listed down a column?
That answer may effect how to try craft a solution for you.

Also, the only way I can really think of how to do this is to use VBA.
Is that acceptable?
The data is loaded from power querry into a table. I used a simplified example which was of course not formated as table.
VBA is great!
Thank you!
 
Upvote 0
Hmmm... This is going to be very difficult, especially working with tables (I really hate trying to move around records in a table!).
I am not sure I can think up of a good way to do this.

There are also some details which need to be worked out still, as your simple example doesn't cover a lot of things.
So here are some other questions:

1. How many different item numbers are there in the list?

2. Are they always sorted by this, so all the records form each individual item number will be bunched together?

3. Is the first number for each item always positive?

4. How would you handle a situation like this?
1637281266166.png


If we start of with 20, adding 10 would actually take us further from 0 (20 + 10 = 30), but if we moved up -100, then the sum becomes negative (20 - 100 = -80), and -80 is further from 0 than 30 is.
So do we move up the -100 or not in this case?

5. Any chance we can work with the numbers in a list, instead of a table?

Depending on your answers to the questions, I might be able to come up with a rather brute force way of doing this, but I cannot make any promises.
 
Upvote 0
Hmmm... This is going to be very difficult, especially working with tables (I really hate trying to move around records in a table!).
I am not sure I can think up of a good way to do this.

There are also some details which need to be worked out still, as your simple example doesn't cover a lot of things.
So here are some other questions:

1. How many different item numbers are there in the list?

2. Are they always sorted by this, so all the records form each individual item number will be bunched together?

3. Is the first number for each item always positive?

4. How would you handle a situation like this?
View attachment 51616

If we start of with 20, adding 10 would actually take us further from 0 (20 + 10 = 30), but if we moved up -100, then the sum becomes negative (20 - 100 = -80), and -80 is further from 0 than 30 is.
So do we move up the -100 or not in this case?

5. Any chance we can work with the numbers in a list, instead of a table?

Depending on your answers to the questions, I might be able to come up with a rather brute force way of doing this, but I cannot make any promises.
Hello,

1. How many different item numbers are there in the list? - Currently the table has ~16.000 lines, and ~ 2.000 Item numbers

2. Are they always sorted by this, so all the records form each individual item number will be bunched together? - Yes, the sorting is done in Power Query, if any other sorting or helping column is required, please let me know.

3. Is the first number for each item always positive? - Yes

4. How would you handle a situation like this?
View attachment 51616

If we start of with 20, adding 10 would actually take us further from 0 (20 + 10 = 30), but if we moved up -100, then the sum becomes negative (20 - 100 = -80), and -80 is further from 0 than 30 is.
So do we move up the -100 or not in this case?

- I would have the following order: 20; -100; 10


5. Any chance we can work with the numbers in a list, instead of a table? - Yes, I can convert the table to list before running the VBA.

Thanks again! It will be a great help for me
 
Upvote 0
4. How would you handle a situation like this?
View attachment 51616

If we start of with 20, adding 10 would actually take us further from 0 (20 + 10 = 30), but if we moved up -100, then the sum becomes negative (20 - 100 = -80), and -80 is further from 0 than 30 is.
So do we move up the -100 or not in this case?

- I would have the following order: 20; -100; 10
Hmmm... If that is the case, the example you posted does NOT follow the logic you explained.

In your expected output, you show 200 in the 4th spot. If we add the Running Total column to the right of this, we can see the values:
1637328054326.png


However, if we use the -30 there instead of 200, look what our Running Total is:
1637328102452.png


-50 is a lot closer to 0 than 180 is!

So, is there a mistake in the example you posted, or a mistake in the logic you posted/explained?

We need to completely and clearly understand the logic of how this is supposed to work if we want to try to come up with a solution.
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,689
Members
449,117
Latest member
Aaagu

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