Counts Number of Rows Between 2 Cells with Equal Value on Separate Rows

Poker Joe

New Member
Joined
Feb 12, 2015
Messages
30
Office Version
  1. 2019
Platform
  1. Windows
What I have:
I have values in cells C2:G601
That's 600 rows of 5 cells in each row
The values in the cells come from 43 product ID numbers. Ex, AB1, AB2, CR5, etc.

What I humbly request:
A macro (For Each? doesn't have to be) comparing (=) each cell.value to the cells.values above it, as far above as it needs to go within the range Cells (3, 3) , Cells (LastRow, 7) to find an equal value.

On the first match of equal cell.value
count the rows between each cell value

Offset the Row Count results to Cells (0, 10) For Each cell

If no equal value is found for a cell, then leave the Row Count result in cells (0, 10) blank or " " and go to next cell.

Some cells values will equal another cell value in the row above it meaning there is no row count, the Row Count results should be 0 or "0"

Stop finding equal cell.values after the first match is found and row count between cell.values has been counted and results offset, just go to the next cell

ChatGPT Attempt:

VBA Code:
Sub CountRowsBetweenEqualCells()

'Define variables
Dim rng As Range
Dim r As Long
Dim c As Long
Dim lastRow As Long

'Set range
Set rng = Range("C2:G601")

'Loop through each row
For r = 1 To rng.Rows.Count
    'Loop through each cell in row
    For c = 1 To rng.Columns.Count
        'Check if value of current cell is equal to value of cell above
        If rng.Cells(r, c).Value = rng.Cells(r - 1, c).Value Then
            'If equal, calculate rows between current cell and cell above
            lastRow = r - r - 1
            'Offset results to column 10 for each cell
            rng.Cells(0, 10).Offset(r, c).Value = lastRow
        Else
            'If not equal, set results to blank or ""
            rng.Cells(0, 10).Offset(r, c).Value = ""
        End If
    Next c
Next r

End Sub

The macro above leaves all result cells blank except any cell that has an equal value in the first row above it. The macro gives the result as -1,
it should be 0, since there are no rows to count between equal value cells. It only looks for a match in the row above it when there is a match 2 or more rows above it.

Can someone be kind enough to correct the AI?
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
If no equal value is found for a cell, then leave the Row Count result in cells (0, 10) blank or " " and go to next cell.
Some cells values will equal another cell value in the row above it meaning there is no row count, the Row Count results should be 0 or "0"
Stop finding equal cell.values after the first match is found and row count between cell.values has been counted and results offset, just go to the next cell
Some details are confusing to me.

Check my following example and tell me if it is correct:
Dante Amor
ACDEFGHMNOPQ
1
2DA22AM7AM5AM19AM5
3AM1DA19DA6DA20AM2
4AM1DA1DA18DA11AM140
5AM21AM15DA6DA10DA41
6DA20AM16DA10DA13AM53
7AM3AM9AM14DA14DA13
Hoja1


Try the following macro, maybe it requires some adjustment.
In this case, you should give examples and explain the results you need (use XL2BB tool to give examples)
VBA Code:
Sub CountRowsBetweenEqualCells()
  'Define variables
  Dim rng As Range
  Dim r As Long, c As Long, i As Long, n As Long
  Dim a, b
 
  Set rng = Range("C2:G601")                'Set range
 
  For c = 1 To rng.Columns.Count            'Loop through each cell in row
    For r = rng.Rows.Count To 1 Step -1     'Loop through each row
      a = rng.Cells(r, c).Value
      n = -1
      For i = r - 1 To 1 Step -1            'Loop as far above as it needs to go
        b = rng.Cells(i, c).Value
        n = n + 1
        If a = b Then
          rng.Cells(0, 10).Offset(r, c).Value = n
          Exit For                          'Stop finding equal, just go to the next cell
        End If
      Next
    Next r
  Next c
End Sub

If you need to count previous rows then change n = -1 to n = 0

----- --
Note: The macro runs immediately with 5 columns and 600 records. But if the amount of data increases, let me know to modify the code and do it with arrays, then it will be faster, but with the current amount of data I don't see any problems.
----- --
NOTE XL2BB:
For the future, it would help greatly if you could give us the sample data in a form that we can copy to test with, rather that a picture.
MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in
Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
----- --
 
Last edited:
Upvote 0
Wow!

Dante,

It's beautiful. Almost perfect!!

It appears the macro test the value to equal another value within the same column versus last occurrence regardless of column.

Is there a way to modify the macro to count the rows between the last occurence regardless whether it's in the same column or not?

I did try changing x = -1 to x = 0 but that did not give me the result I was looking for.

I appreciate your efforts.

IDDateMTWThFMTWThF
104/25/2021da9kl16mb8ys6ql3
204/26/2021ab2ab3ave5mb12ql30
304/27/2021ve9pl5th5mb12ys40
404/28/2021ab2ve7pl6mb10ql311
504/29/2021da8kl16pl4mb8ys53
604/30/2021ab2ve9da7pl6pl71
705/01/2021ab2ve8th6th9ys403
805/02/2021ve7ve8da8th5ys60
905/03/2021ab4ab5th6mb10ql3144
1005/04/2021ab4kl12th9ys5ys601
1105/05/2021da9kl14kl15pl3pl59
1205/06/2021ab4pl4pl6mb10ys7172
1305/07/2021da9th6th9mb10ql4120
1405/08/2021ab4kl13th7ys6ql41120(Should be 3, ys6 has 3 rows between the last occurrence)
1505/09/2021ab1ab4ve8da11th9
1605/10/2021ab3da8da9th9mb88(Should be 0, th9 has 0 rows between the last occurrence)
1705/11/2021da7kl14kl15mb11ys6556
1805/12/2021ab5da11kl12mb11mb120
[XD=h:c|ch:18|fz:10pt]5[/XD][XD=h:c|fz:10pt|tx:44315]04/29/2021[/XD][XD=h:c|fz:10pt]da8[/XD][XD=h:c|fz:10pt]kl16[/XD][XD=h:c|fz:10pt]pl4[/XD][XD=h:c|fz:10pt]mb8[/XD][XD=h:c|fz:10pt]ys5[/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD=h:c|fz:10pt]3[/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][/XR][XR][XD=h:c|ch:18|fz:10pt]6[/XD][XD=h:c|fz:10pt|tx:44316]04/30/2021[/XD][XD=h:c|fz:10pt]ab2[/XD][XD=h:c|fz:10pt]ve9[/XD][XD=h:c|fz:10pt]da7[/XD][XD=h:c|fz:10pt]pl6[/XD][XD=h:c|fz:10pt]pl7[/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD=h:c|fz:10pt]1[/XD][XD][/XD][XD=h:c|fz:10pt] [/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][/XR][XR][XD=h:c|ch:18|fz:10pt]7[/XD][XD=h:c|fz:10pt|tx:44317]05/01/2021[/XD][XD=h:c|fz:10pt]ab2[/XD][XD=h:c|fz:10pt]ve8[/XD][XD=h:c|fz:10pt]th6[/XD][XD=h:c|bc:FFC000|fz:10pt]th9[/XD][XD=h:c|fz:10pt]ys4[/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD=h:c|fz:10pt]0[/XD][XD][/XD][XD][/XD][XD][/XD][XD=h:c|fz:10pt]3[/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][/XR][XR][XD=h:c|ch:18|fz:10pt]8[/XD][XD=h:c|fz:10pt|tx:44318]05/02/2021[/XD][XD=h:c|fz:10pt]ve7[/XD][XD=h:c|fz:10pt]ve8[/XD][XD=h:c|fz:10pt]da8[/XD][XD=h:c|fz:10pt]th5[/XD][XD=h:c|fz:10pt]ys6[/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD=h:c|fz:10pt]0[/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][/XR][XR][XD=h:c|ch:18|fz:10pt]9[/XD][XD=h:c|fz:10pt|tx:44319]05/03/2021[/XD][XD=h:c|fz:10pt]ab4[/XD][XD=h:c|fz:10pt]ab5[/XD][XD=h:c|fz:10pt]th6[/XD][XD=h:c|fz:10pt]mb10[/XD][XD=h:c|fz:10pt]ql3[/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD=h:c|fz:10pt]1[/XD][XD=h:c|fz:10pt]4[/XD][XD=h:c|fz:10pt]4[/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][/XR][XR][XD=h:c|ch:18|fz:10pt]10[/XD][XD=h:c|fz:10pt|tx:44320]05/04/2021[/XD][XD=h:c|fz:10pt]ab4[/XD][XD=h:c|fz:10pt]kl12[/XD][XD=h:c|fz:10pt]th9[/XD][XD=h:c|fz:10pt]ys5[/XD][XD=h:c|bc:92D050|fz:10pt]ys6[/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD=h:c|fz:10pt]0[/XD][XD][/XD][XD][/XD][XD][/XD][XD=h:c|fz:10pt]1[/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][/XR][XR][XD=h:c|ch:18|fz:10pt]11[/XD][XD=h:c|fz:10pt|tx:44321]05/05/2021[/XD][XD=h:c|fz:10pt]da9[/XD][XD=h:c|fz:10pt]kl14[/XD][XD=h:c|fz:10pt]kl15[/XD][XD=h:c|fz:10pt]pl3[/XD][XD=h:c|fz:10pt]pl5[/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD=h:c|fz:10pt]9[/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][/XR][XR][XD=h:c|ch:18|fz:10pt]12[/XD][XD=h:c|fz:10pt|tx:44322]05/06/2021[/XD][XD=h:c|fz:10pt]ab4[/XD][XD=h:c|fz:10pt]pl4[/XD][XD=h:c|fz:10pt]pl6[/XD][XD=h:c|fz:10pt]mb10[/XD][XD=h:c|fz:10pt]ys7[/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD=h:c|fz:10pt]1[/XD][XD][/XD][XD=h:c|fz:10pt]7[/XD][XD=h:c|fz:10pt]2[/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][/XR][XR][XD=h:c|ch:18|fz:10pt]13[/XD][XD=h:c|fz:10pt|tx:44323]05/07/2021[/XD][XD=h:c|fz:10pt]da9[/XD][XD=h:c|fz:10pt]th6[/XD][XD=h:c|fz:10pt]th9[/XD][XD=h:c|fz:10pt]mb10[/XD][XD=h:c|fz:10pt]ql4[/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD=h:c|fz:10pt]1[/XD][XD][/XD][XD=h:c|fz:10pt]2[/XD][XD=h:c|fz:10pt]0[/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][/XR][XR][XD=h:c|ch:18|fz:10pt]14[/XD][XD=h:c|fz:10pt|tx:44324]05/08/2021[/XD][XD=h:c|fz:10pt]ab4[/XD][XD=h:c|fz:10pt]kl13[/XD][XD=h:c|fz:10pt]th7[/XD][XD=h:c|bc:92D050|fz:10pt]ys6[/XD][XD=h:c|fz:10pt]ql4[/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD=h:c|fz:10pt]1[/XD][XD][/XD][XD][/XD][XD=h:c|bc:92D050|fz:10pt]12[/XD][XD=h:c|fz:10pt]0[/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][/XR][XR][XD=h:c|ch:18|fz:10pt]15[/XD][XD=h:c|fz:10pt|tx:44325]05/09/2021[/XD][XD=h:c|fz:10pt]ab1[/XD][XD=h:c|fz:10pt]ab4[/XD][XD=h:c|fz:10pt]ve8[/XD][XD=h:c|fz:10pt]da11[/XD][XD=h:c|bc:FFC000|fz:10pt]th9[/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][/XR][XR][XD=h:c|ch:18|fz:10pt]16[/XD][XD=h:c|fz:10pt|tx:44326]05/10/2021[/XD][XD=h:c|fz:10pt]ab3[/XD][XD=h:c|fz:10pt]da8[/XD][XD=h:c|fz:10pt]da9[/XD][XD=h:c|bc:FFC000|fz:10pt]th9[/XD][XD=h:c|fz:10pt]mb8[/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD=h:c|bc:FFC000|fz:10pt]8[/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][/XR][XR][XD=h:c|ch:18|fz:10pt]17[/XD][XD=h:c|fz:10pt|tx:44327]05/11/2021[/XD][XD=h:c|fz:10pt]da7[/XD][XD=h:c|fz:10pt]kl14[/XD][XD=h:c|fz:10pt]kl15[/XD][XD=h:c|fz:10pt]mb11[/XD][XD=h:c|fz:10pt]ys6[/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD=h:c|fz:10pt]5[/XD][XD=h:c|fz:10pt]5[/XD][XD][/XD][XD=h:c|fz:10pt]6[/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][/XR][XR][XD=h:c|ch:18|fz:10pt]18[/XD][XD=h:c|fz:10pt|tx:44328]05/12/2021[/XD][XD=h:c|fz:10pt]ab5[/XD][XD=h:c|fz:10pt]da11[/XD][XD=h:c|fz:10pt]kl12[/XD][XD=h:c|fz:10pt]mb11[/XD][XD=h:c|fz:10pt]mb12[/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD=h:c|fz:10pt]0[/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][/XR][XR][XD=h:c|ch:18|fz:10pt]19[/XD][XD=h:c|fz:10pt|tx:44329]05/13/2021[/XD][XD=h:c|fz:10pt]ab4[/XD][XD=h:c|fz:10pt]kl12[/XD][XD=h:c|fz:10pt]pl7[/XD][XD=h:c|fz:10pt]th6[/XD][XD=h:c|fz:10pt]ys6[/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD=h:c|fz:10pt]4[/XD][XD=h:c|fz:10pt]8[/XD][XD][/XD][XD][/XD][XD=h:c|fz:10pt]1[/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][/XR][XR][XD=h:c|ch:18|fz:10pt]20[/XD][XD=h:c|fz:10pt|tx:44330]05/14/2021[/XD][XD=h:c|fz:10pt]ve6[/XD][XD=h:c|fz:10pt]da8[/XD][XD=h:c|fz:10pt]ys4[/XD][XD=h:c|fz:10pt]ys7[/XD][XD=h:c|fz:10pt]ql5[/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD=h:c|fz:10pt]3[/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][/XR][XR][XD=h:c|ch:18|fz:10pt]21[/XD][XD=h:c|fz:10pt|tx:44331]05/15/2021[/XD][XD=h:c|fz:10pt]ve7[/XD][XD=h:c|fz:10pt]kl15[/XD][XD=h:c|fz:10pt]pl5[/XD][XD=h:c|fz:10pt]mb11[/XD][XD=h:c|fz:10pt]mb12[/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD=h:c|fz:10pt]12[/XD][XD][/XD][XD][/XD][XD=h:c|fz:10pt]2[/XD][XD=h:c|fz:10pt]2[/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][XD][/XD][/XR][/RANGE]
 
Upvote 1
It appears the macro test the value to equal another value within the same column versus last occurrence regardless of column.
I used arrays for the loops.
Execution is immediate for all 5 columns, 600 rows, loop back and search all 5 columns.

Try this:
VBA Code:
Sub CountRowsBetweenEqualCells()
  'Define variables
  Dim rng As Range
  Dim i As Long, j As Long, k As Long, m As Long, n As Long
  Dim a As Variant, b As Variant, s1 As Variant, s2 As Variant
  Dim bln As Boolean
  
  Set rng = Range("C2:G601")                'Set range
  a = rng.Value
  ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
  
  For j = 1 To UBound(a, 2)                 'Loop through each cell in row
    For i = UBound(a, 1) To 1 Step -1       'Loop through each row
      s1 = a(i, j)
      n = 0
      For k = i - 1 To 1 Step -1            'Loop as far above as it needs to go
        bln = False
        For m = 1 To UBound(a, 2)           'Loop each column
          s2 = a(k, m)
          If s1 = s2 Then
            bln = True
            Exit For                        'exit loop m
          End If
        Next m
        If bln Then
          b(i, j) = n
          Exit For                          'exit loop k
        End If
        n = n + 1
      Next k
    Next i
  Next
  
  Range("M2").Resize(UBound(b, 1), UBound(b, 2)).Value = b
End Sub


Example:
Dante Amor
ACDEFGHMNOPQ
588AM9AM17AM17DA2AM19145508
589DA5AM12AM15AM13DA9811075
590AM17DA17AM21AM3DA211141023
591AM19AM19AM18DA10AM19229142
592DA12AM6DA22AM16AM1142810810
593AM20AM19DA9AM19DA3281317
594DA18AM2AM2AM4AM4719191111
595DA1DA11AM12DA3DA9815511
596DA4AM5DA20DA20AM1415138811
597DA8AM4AM1AM10DA2110219116
598DA15AM4DA14DA16AM227024253
599DA20AM10AM2AM12AM1421032
600DA10AM18AM15DA11AM3881049
601AM15DA20DA2AM19DA15011272
Hoja1
 
Upvote 1
Solution
Wow! Just Wow! Works perfect! 100%!
For the life of me I can't wrap my head around how you guys write these macros. I can't express my gratitude enough. You saved me a headache and lots of time. I wish they had a tip jar on this site. Sending my best karma. Thank You Dante.
 
Upvote 1

Forum statistics

Threads
1,223,227
Messages
6,170,853
Members
452,361
Latest member
d3ad3y3

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