Subtracting every other cell in VBA

G_Chas

New Member
Joined
Mar 14, 2022
Messages
3
Office Version
  1. 2013
Platform
  1. Windows
Hello,
Im new in programming and specifically in VBA. I have a problem where i have 1 huge column (E) and i need to subtract each cell with the sum of other previous ones, in every other cell. In particular, i need every north (N) pile to be the number written, minus the sum of all previous north piles, and for south piles the exact same thing. Does anyone have a clue about that?
North-South piles row_C05.png
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
I believe you are showing us the input. Could you show us an example of what you want for the output?
 
Upvote 0
Hello @Ezguy4u, that is the input, correct. For example the 315N pile (986) needs to be =315N(986)-314N(272)-313N(226)-312N(222) and for the south pile the exact same thing(south piles only). That subtraction needs to occur every time there a north pile (or south) and subtract every north pile (or south) up to this point.
 
Upvote 0
Welcome to the MrExcel board!

Is this what you mean?

VBA Code:
Sub NS()
  Dim a As Variant
  Dim i As Long, NTot As Long, STot As Long, tmp As Long
  
  a = Range("D5", Range("D" & Rows.Count).End(xlUp)).Value
  NTot = a(1, 1)
  STot = a(2, 1)
  For i = 3 To UBound(a) Step 2
    tmp = a(i, 1)
    a(i, 1) = a(i, 1) - NTot
    NTot = NTot + tmp
    tmp = a(i + 1, 1)
    a(i + 1, 1) = a(i + 1, 1) - STot
    STot = STot + tmp
  Next i
  Range("E5").Resize(UBound(a)).Value = a
End Sub

My sample data in columns A:D, code results in column E (for checking - they could be written back to column D if you want)

G_Chas.xlsm
ABCDE
1
2
3
4
5N222222
6S229229
7N2264
8S222-7
9N272-176
10S725274
11N986266
12S998-178
13N1257-449
14S1262-912
15N1538-1425
16S1549-1887
Sheet1


If this is not what you want, please provide the expected results (preferably with XL2BB) and explain again
 
Upvote 0
Hello @Peter_SSs, thank you!! This exactly what i want but the programm needs to subtract the already subtracted value from the previous cell. For example:
E12 = D12(998) - [D10(725) - D8(222) - D6(229)] - D8(222) - D6(229).
By hand this results in 273 but in the program it gives back -178.
I hope I made it more clear and not the other way around.
Thank you for your time.
 
Upvote 0

Forum statistics

Threads
1,224,900
Messages
6,181,636
Members
453,059
Latest member
jkevin

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