How to count the number of rows between two values

Kishan

Well-known Member
Joined
Mar 15, 2011
Messages
1,648
Office Version
  1. 2010
Platform
  1. Windows
Hi,

Column R there is numbers with gaps (only 1 to 9), I want to count number of rows (in every intervals) between values 1 to 9 and result in range U6 down and in last interval count rows “1” to data end of the column Q
Example...


Book1
QRSTU
1
2
3
4Rows Count
51 to 9
6111st Time26
7222nd Time19
833rd Time
943
1054
1165
127
1386
149
1510
1611
1712
1813
1914
2015
2116
2217
2318
2419
25207
2621
2722
2823
29248
3025
31269
32271
33282
3429
3530
3631
3732
38333
39344
40355
41366
42377
4338
4439
4540
4641
4742
4843
4944
5045
Sheet1


VBA solution if possible

Thank you in advance
Kishan
 
Try this

Code:
Sub t()
Dim fn As Range, lr As Long, a As Long
With ActiveSheet
    lr = .Cells(Rows.Count, "R").End(xlUp).Row
    a = 6
    Set fn = .Range("R6:R" & lr).Find("9", , xlValues, xlWhole)
        If Not fn Is Nothing Then
            fAdr = fn.Address
            Do
                If .Range("U6") = "" Then
                    .Range("U6") = (fn.Row - a) + 1
                Else
                    .Cells(Rows.Count, "U").End(xlUp)(2) = (fn.Row - a) + 1
                End If
                a = fn.Row + 1
                Set fn = .Range("R6:R" & lr).FindNext(fn)
                If fn.Address = fAdr Then
                    .Cells(Rows.Count, "U").End(xlUp)(2) = (lr - a) + 1
                End If
            Loop While fn.Address <> fAdr
        End If
End With
End Sub
 
Upvote 0
Thank you JLGWhiz, almost working perfect working only as I request last segment result could be as long as data find in column Q, I explain you why macro is resulting U6 = 26 and U7 = 11 (what if 8 does not appear in next x rows I always will get result U = 11 but really current time lap is 19 not 11 so is it possibility to get final segment result U7 = 19 instead 11

Regards,
Kishan
 
Upvote 0
Thank you JLGWhiz, almost working perfect working only as I request last segment result could be as long as data find in column Q, I explain you why macro is resulting U6 = 26 and U7 = 11 (what if 8 does not appear in next x rows I always will get result U = 11 but really current time lap is 19 not 11 so is it possibility to get final segment result U7 = 19 instead 11

Regards,
Kishan

Sorry, I based the last row on Column R, because that was the one that held the series 1 to 9. I have changed the last row reference to column Q. See if this works better.

Code:
Sub t2()
Dim fn As Range, lr As Long, a As Long
With ActiveSheet
    lr = .Cells(Rows.Count, "Q").End(xlUp).Row
    a = 6
    Set fn = .Range("R6:R" & lr).Find("9", , xlValues, xlWhole)
        If Not fn Is Nothing Then
            fAdr = fn.Address
            Do
                If .Range("U6") = "" Then
                    .Range("U6") = (fn.Row - a) + 1
                Else
                    .Cells(Rows.Count, "U").End(xlUp)(2) = (fn.Row - a) + 1
                End If
                a = fn.Row + 1
                Set fn = .Range("R6:R" & lr).FindNext(fn)
                If fn.Address = fAdr Then
                    .Cells(Rows.Count, "U").End(xlUp)(2) = (lr - a) + 1
                End If
            Loop While fn.Address <> fAdr
        End If
End With
End Sub
 
Upvote 0
This is amazing - I was looking for something almost identical to this, and I have been able to manipulate it ALMOST to what I need.

Would anyone be able to help me put this through a loop, so that assuming everything is identical to above, but then there are multiple columns of data (ie, column R repeats to column S, T, U, V... etc - for a lot of columns), that the loop does the above macro, then moves on to the next column, then the next, then the next etc.

I have moved the results of the data from Cell U6 to a separate tab, but would like the results from Column R to update in Column R on a new tab, then the results from column S to update column S in a separate tab, and so on.

Hopefully that makes sense! Thanks in advance
 
Upvote 0

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