• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 53
  • Last Modified:

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()
ThisWorkbook.Sheets(1).Activate

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
        
    Next
    Cells(i, 11).Value = count
Next

End Sub

Open in new window


Is there any other way to get the same count, or a way to get around the crashing?
0
Pearlyn Tan
Asked:
Pearlyn Tan
1 Solution
 
NorieVBA ExpertCommented:
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

0
 
Fabrice LambertFabrice LambertCommented:
Hi,

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
        Next
        dataTarget(i, 1) = count
    Next
    
    dataRange.Value = dataTarget
    Set dataRange = Nothing
    Set ws = Nothing
    Set wb = Nothing
End Sub

Open in new window

0
 
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now