PC Review


Reply
Thread Tools Rate Thread

Calculating the difference between cell values in different rows - Loop

 
 
joecrabtree
Guest
Posts: n/a
 
      29th Nov 2006
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 With Quote
 
 
 
 
Nigel
Guest
Posts: n/a
 
      29th Nov 2006
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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 With Quote
 
joecrabtree
Guest
Posts: n/a
 
      30th Nov 2006
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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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 With Quote
 
Bernard Liengme
Guest
Posts: n/a
 
      30th Nov 2006
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>> > 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 With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
SUMPRODUCT calculating difference between column values Mike Microsoft Excel Worksheet Functions 8 28th Feb 2009 07:39 AM
Calculating difference between two date values Bren Microsoft Access 4 10th Apr 2008 09:22 PM
Calculating difference of two values as a percentage =?Utf-8?B?dm9sZ2Fs?= Microsoft Access 2 18th Oct 2007 05:06 PM
Calculating difference between field values for different records =?Utf-8?B?U3RldmVT?= Microsoft Access Queries 1 1st May 2005 05:33 AM
Calculating the difference in two values Tony Williams Microsoft Access Reports 6 16th Dec 2004 02:12 PM


Features
 

Advertising
 

Newsgroups
 


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