View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
John Green[_2_] John Green[_2_] is offline
external usenet poster
 
Posts: 58
Default VBA code to calculate the difference between rows

Desmond,

The following code subtracts the cells in row 2 from the cells in row 1 and places the results in an array:

Sub Difference()
Dim i As Integer
Dim dResult(1 To 10) As Double

For i = 1 To 10
dResult(i) = Cells(1, i).Value - Cells(2, i).Value
Next i
End Sub


--

John Green - Excel MVP
Sydney
Australia


"Desmond" wrote in message ...
Hello,
I have two rows of numbers in cells. I need the VBA code
that will subtract each cell value in one line from the
cell in the same column in the other line.

I created a macro that subtracts the first cell and
copies across the row, but with the worksheet protected
this does not work. I tried a recorded macro subtracting
each cell separately, and this seems to work. So I
believe I need a loop structure, and I am having trouble
with this.

Your assistance would be appreciated. I know little of
programming, so your help would be valuable

Thanks

desmond