Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 111
Default Calculating the difference between cell values in different rows - Loop

To All,

I have a list of data in column A: Eg:

1
2
3
4
5
5

I wish to calculate the difference between rows, i.e. ( 2-1), (3-2)
etc, and be able to loop this for however many rows of data I have -
which will vary. I then want to output the ranges to a sheet called
sheet1.

Any help would be much appreciated.

Thanks in advance.

Regards

Joseph Crabtree

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 923
Default Calculating the difference between cell values in different rows - Loop

Assuming your range is in sheet2, change below if not, then use this

Sub DiifList()
Dim xlr As Long, xr As Long, opr As Long
Sheets("Sheet1").Columns(1).ClearContents
With Sheets("Sheet2")
xlr = .Cells(.Rows.Count, "A").End(xlUp).Row
opr = 1
For xr = 1 To xlr - 1
Sheets("Sheet1").Cells(opr, 1) = .Cells(xr + 1, 1) - .Cells(xr, 1)
opr = opr + 1
Next
End With
End Sub



--
Cheers
Nigel



"joecrabtree" wrote in message
oups.com...
To All,

I have a list of data in column A: Eg:

1
2
3
4
5
5

I wish to calculate the difference between rows, i.e. ( 2-1), (3-2)
etc, and be able to loop this for however many rows of data I have -
which will vary. I then want to output the ranges to a sheet called
sheet1.

Any help would be much appreciated.

Thanks in advance.

Regards

Joseph Crabtree



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 111
Default Calculating the difference between cell values in different rows - Loop

Hi Nigel,

Thats great thanks. Is there anyway I can get it to just return
positive numbers. I.e if The difference is 5-10 then the answer would
bed 5 not -5?

Thanks

Joseph Crabtree


Nigel wrote:
Assuming your range is in sheet2, change below if not, then use this

Sub DiifList()
Dim xlr As Long, xr As Long, opr As Long
Sheets("Sheet1").Columns(1).ClearContents
With Sheets("Sheet2")
xlr = .Cells(.Rows.Count, "A").End(xlUp).Row
opr = 1
For xr = 1 To xlr - 1
Sheets("Sheet1").Cells(opr, 1) = .Cells(xr + 1, 1) - .Cells(xr, 1)
opr = opr + 1
Next
End With
End Sub



--
Cheers
Nigel



"joecrabtree" wrote in message
oups.com...
To All,

I have a list of data in column A: Eg:

1
2
3
4
5
5

I wish to calculate the difference between rows, i.e. ( 2-1), (3-2)
etc, and be able to loop this for however many rows of data I have -
which will vary. I then want to output the ranges to a sheet called
sheet1.

Any help would be much appreciated.

Thanks in advance.

Regards

Joseph Crabtree


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,393
Default Calculating the difference between cell values in different rows - Loop

Just modify one line to read
Sheets("Sheet1").Cells(opr, 1) = Abs(.Cells(xr + 1, 1) - .Cells(xr, 1))
best wishes
--
Bernard V Liengme
www.stfx.ca/people/bliengme
remove caps from email

"joecrabtree" wrote in message
ups.com...
Hi Nigel,

Thats great thanks. Is there anyway I can get it to just return
positive numbers. I.e if The difference is 5-10 then the answer would
bed 5 not -5?

Thanks

Joseph Crabtree


Nigel wrote:
Assuming your range is in sheet2, change below if not, then use this

Sub DiifList()
Dim xlr As Long, xr As Long, opr As Long
Sheets("Sheet1").Columns(1).ClearContents
With Sheets("Sheet2")
xlr = .Cells(.Rows.Count, "A").End(xlUp).Row
opr = 1
For xr = 1 To xlr - 1
Sheets("Sheet1").Cells(opr, 1) = .Cells(xr + 1, 1) - .Cells(xr, 1)
opr = opr + 1
Next
End With
End Sub



--
Cheers
Nigel



"joecrabtree" wrote in message
oups.com...
To All,

I have a list of data in column A: Eg:

1
2
3
4
5
5

I wish to calculate the difference between rows, i.e. ( 2-1), (3-2)
etc, and be able to loop this for however many rows of data I have -
which will vary. I then want to output the ranges to a sheet called
sheet1.

Any help would be much appreciated.

Thanks in advance.

Regards

Joseph Crabtree




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I loop through a the values in multiple rows EMarre Excel Discussion (Misc queries) 6 May 13th 10 01:50 PM
Calculating different values and percentages in rows ColonelK0rn Excel Worksheet Functions 4 May 30th 09 07:08 PM
summing difference cell values across worksheets Kgoodman Excel Worksheet Functions 4 March 2nd 09 11:37 PM
SUMPRODUCT calculating difference between column values Mike Excel Worksheet Functions 8 February 28th 09 07:39 AM
error calculating negativ difference time values Xavier Excel Programming 2 March 26th 06 11:51 PM


All times are GMT +1. The time now is 11:49 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"