View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
[email protected] matthew.pierson@gmail.com is offline
external usenet poster
 
Posts: 2
Default macro to subtract across columns

Hmmm... neither work as expected... but maybe if I give more
details... here is what my spreadsheet looks like:

J K
L M
N O P
(41,328.80) - 11,755.73 17,197.26 5,143.26 12,419.83
-


So what I want to happen is for the amount in J to be added to P to
reduce it to zero which it already is, then the remainder to be added
to O to reduce it to zero... and so on until it gets to L where the
amount would be 5,187.28




On Feb 21, 12:23 pm, Tom Ogilvy
wrote:
Sub SubtractValues()
Dim cell As Range, cell1 As Range
Dim v, v1, i As Long
For Each cell In Range("A1:A20")
v = cell.Value
If IsNumeric(v) Then
If v < 0 Then
For i = 5 To 2 Step -1
Set cell1 = Cells(cell.Row, i)
If IsNumeric(cell1) Then
If v < 0 Then
If cell1 + v = 0 Then
cell1 = cell1 + v
v = 0
Else
v = v + cell1
cell1 = 0
End If
End If
End If
Next i
Cells(cell.Row, 6) = v
End If
End If
Next
End Sub

--
Regards,
Tom Ogilvy



" wrote:
Hello,


I'm trying to automate a method of summing across columns. To be
exact, let's say I have 5 columns (A B C D E), the first of which
MIGHT contain a negative value (it may also contain a positive value,
and B C D E might have a zero or some positive value. What I want to
do is... IF A has a negative value I want it to add it to E (to reduce
it), if E becomes zero and there is still some left from A, I want it
to add the remainder to D... then C.... then B. Essentially I want
the values of E D C B to be reduced, in turn, by the value that is in
A (but only if A has a negative value).


I'm hoping this makes sense... and I'm hoping someone out there can
figure this out.


Cheers.- Hide quoted text -


- Show quoted text -