View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Tom Ogilvy Tom Ogilvy is offline
external usenet poster
 
Posts: 6,953
Default macro to subtract across columns

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.