For Loop Count for large data

Hello, I'm trying to do a count for each row of my data with reference to all other rows in the dataset. Column 6 stores a date, and I want to count how many other rows (j, 6) have a date that the same or before the row's date (i, 6), and store this count in column K. However, it doesn't run well with many rows of data. I have about 130,000 rows but on my incremental testing it only runs well up to about 6000 rows. Any more and excel will crash.

Sub count()

Dim lrow As Long
lrow = Cells(Rows.count, 1).End(xlUp).Row

Dim count As Long

Dim i As long
Dim j As long

For i = 2 To lrow
    count = 0
    For j = 2 To lrow
        If Cells(j, 6).Value < Cells(i, 6).Value Then
        count = count + 1
        End If
    Cells(i, 11).Value = count

End Sub

Open in new window

Is there any other way to get the same count, or a way to get around the crashing?
Pearlyn TanAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NorieAnalyst Assistant Commented:
Couldn't you use COUNTIF?
Dim lRow As Long

    With Sheets(1)
        lRow = .Cells(Rows.Count, 1).End(xlUp).Row
        .Range(.Cells(2, 11), .Cells(lRow, 11)).Formula = "=COUNTIF($F$2:$F" & lRow & ",""<=""&F2)"
    End With

Open in new window

Fabrice LambertFabrice LambertCommented:

Instead of accessing cells directly (wich is time and ressources consuming), what about affecting your ranges to an array ?
It is about 20 times faster.
Option Explicit

Public Sub count()
    Dim wb As Excel.Workbook
    Set wb = ThisWorkbook
    Dim ws As Excel.Worksheet
    Set ws = wb.Worksheets(1)
    Dim maxRow As Long
    maxRow = ws.Cells(ws.Rows.count, 1).End(xlUp).Row

    Dim dataRange As Excel.Range
    Set dataRange = ws.Range("F2:F" & maxRow)
        '// transfert data to a 2D array
    Dim dataSource() As Variant
    dataSource = dataRange.Value
        '// transfert data to a 2D array
    Set dataRange = ws.Range("K2:K" & maxRow)
    Dim dataTarget() As Variant
    dataTarget = dataRange.Value
        '// perform the counts
    Dim i As Long
    For i = LBound(dataSource, 1) To UBound(dataSource, 1)
        Dim count As Long
        count = 0
        Dim j As Long
        For j = LBound(dataSource, 1) To UBound(dataSource, 1)
            If (dataSource(j, 1) < dataSource(i, 1)) Then
                count = count + 1
            End If
        dataTarget(i, 1) = count
    dataRange.Value = dataTarget
    Set dataRange = Nothing
    Set ws = Nothing
    Set wb = Nothing
End Sub

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Pearlyn TanAuthor Commented:
Thanks Fabrice! This worked.
Thank you Norie for your suggestion - not sure where the error was but the countif didn't work well, only showed '0' for every row.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.