Difference between start and end rows of consecutive data

GLIBBY

New Member
Joined
Jun 14, 2010
Messages
37
Hello. Im trying to write a spreadsheet to accomplish the following function. One column (column a) will be data in which there will be consecutive entires of the same value. There will be a seperate column (column b) of data which is related to the first. I want to create a third column which calculates the difference in the values in column b, which correspond with the first row, and last row of a consecutive block of column A values.

I was thinking the simplest method would be to calculate a given row's difference between the first row of the block. The only problem Im having is determining where a block begins, since the will be multiple occurances of blocks of data.

Any help is much appreciated.
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
you data is like this

Excel Workbook
AB
1hdngahdngb
2a1
3a2
4a3
5a4
6s5
7s6
8s7
9d8
10d9
11d8
12d7
13d6
Sheet1


now try this macro

Code:
Sub test()
Dim r As Range, r1 As Range, c As Range
Set r = Range("A2")
Do
For Each c In Range(r, r.End(xlDown))
If c.Offset(1, 0) <> c Then
Set r1 = c
GoTo Nextstep
End If
Next c
Nextstep:

r.Offset(0, 2) = r1.Offset(0, 1) - r.Offset(0, 1)
Set r = r1.Offset(1, 0)
If r = "" Then Exit Do
Loop
End Sub
Code:
Sub undo()
Range("C1").EntireColumn.Delete
End Sub
 
Upvote 0
Thats close to what I need, but I was looking to make a function so that I dont have to run the macro each time. I thought about it more and i figured that the function can be in a 3rd column (column c) and can search column A for the next instance of a non continuous value. Then, subtract the linked value at the break from the starting value.

I wrote this function to try and do that, but its not working out for me.

Code:
Function continuous(c As Range)

Dim done As Boolean
Dim value As Long, result As Long, r As Long


r = Range(c).Offset(0, 1).value
done = False


Do


 If c.Offset(1, 0) = c And c.Offset(2, 0) = c.Offset(1, 0) Then
    c.Offset(1, 0).Select
    c = Range(ActiveCell)
    done = False
 
 Else
 
 value = c.Offset(1, 1)
 result = value - r
 done = True
 
 End If
 

 
    
Loop Until done = True


continuous = result

End Function
 
Upvote 0
Any thoughts? I figure all I need to do is create a function where you declare a cell, and it finds the last cell that is equal to the declared cell, with consecutive values in between. This seems easier said than done though.
 
Upvote 0
Your function needs to be declared like so:

Code:
Function continuous(ByVal c As Range)


I think this works:

Code:
Function mycontinuous(ByVal cell As Range)

    Set rvala = cell.Offset(0, -1)
    
    
    vala = rvala.value
    
    Do While vala = rvala.value
        Set rvala = rvala.Offset(1)
    Loop

    mycontinuous = rvala.Offset(-1, 1).value - cell.value


End Function
 
Last edited:
Upvote 0
Thats awesome! Exactly what I needed, thanks!

What does the "ByVal" declaration of the function do?
 
Upvote 0
How about the CSE formula
=INDEX(B:B,MAX(($A$1:$A$100=A1)*ROW($A$1:$A$100)),1)-VLOOKUP(A1,A:A,B:B,False)
 
Upvote 0
What does the "ByVal" declaration of the function do?

Don't fully understand the particulars, but it's absence will at times cause an error.

It means, if you change the passed argument within the function, it won't affect that argument's status/value within the calling procedure.
 
Upvote 0

Forum statistics

Threads
1,224,548
Messages
6,179,453
Members
452,915
Latest member
hannnahheileen

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top