Subtract with positive values

Status
Not open for further replies.

abdul7019

New Member
Joined
May 7, 2022
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hello, i rather new vba and i have found great a difficulty to find a solution for this problem.
I have a flat xls table with 100000+ rows and 100+ columns with blank cells, 0 value cells, >0 value cells and <0 cells . What i want to do is a vba code to run through every line and when it finds a <0 value cell to go back and start subtracting this value from the previous cells until it comes to =0 OR >0 (subtract only from the positive ones). Then continue from were it stopped (the negative value) until the end of the line. Then it has to loop for every line until the end. Blank and 0 cells are considered as 0 value.Please see below an example of a test dataset and the desired results. The "tricky" part is in Line 3 where the subtraction leads to negative result since there are no more values to subtract and in this case sets the cell value to 0.


Thank you in advance
Abdul Raheem


L1100500-10100-50
L20200-100-5040-1050
L3300-4001005030
L4100100100100-450100-50
L1_Result1004000500
L2_Result0500030050
L3_Result001005030
L4_Result50000050
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Welcome to the MrExcel board!

I have reproduced your sample data and expected results below. I do not understand the green value.
Since the first negative number in that row is -450, why doesn't that reduce all 4 100 values in A4:D4 to zero just like the -400 in row 3 reduced the 300 value to zero?

abdul7019.xlsm
ABCDEFGHI
1100500-10100-50
20200-100-5040-1050
3300-4001005030
4100100100100-450100-50
5
61004000500
70500030050
8001005030
950000050
Sheet1
 
Upvote 0
Thanks for responding

The macro should go and search for negative value the we have check if any positive value in that row we have to swap with that for each rows we can take the previous value or we can take forward value until it become zero
 
Upvote 0
I'm afraid that did not explain why the green cell is 50.

As I understand it, for row 4 we start at I4 = -50
Moving left we come to 100 in G4 so we reduce G4 by 50 to give 50 and we change I4 to zero because we have used up the -50.
Moving left again we come to -450 in F4.
Moving left we come to 100 in D4 so it goes down to 0 and F4 would change to -350.
Moving left we come to 100 in C4 so it goes down to 0 and F4 would change to -250.
Moving left we come to 100 in B4 so it goes down to 0 and F4 would change to -150.
Moving left we come to 100 in A4 so it goes down to 0 and F4 would change to -50.

Where did I go wrong with that logic?
Why is A4 50 not 0?
And also, why is F4 now 0 and not still -50?
 
Upvote 0
Hmm, it seems your original question in post #1 above is an exact copy of this one.
That thread was answered successfully in post #4 so I am closing this thread.
If your question is actually different - as appears may be the case from post #7 in that other thread - then please start a new question with your own sample data and your own requirements detailed in it.
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,223,956
Messages
6,175,609
Members
452,660
Latest member
Zatman

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