ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   VBA code to calculate the difference between rows (https://www.excelbanter.com/excel-programming/272788-re-vba-code-calculate-difference-between-rows.html)

John Green[_2_]

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





All times are GMT +1. The time now is 05:45 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com