Link to home
Start Free TrialLog in
Avatar of Pearlyn Tan
Pearlyn Tan

asked on

Conditional CountIfs using Arrays

Hello,

I have previously asked a similar question but I'm not sure how to tweak the solution provided. I'm trying to do a count with date reference to all other rows.
Count if column F's date (request date) is between that of column F (request date) and column H (fulfillment date) of every other row --> For each row, I want to count how many other rows are still in the pipeline (requested but has not been fulfilled)

A kind community member gave the following solution that worked (using arrays) but I previously did not ask abut the fulfilment date. Thus it only counts the number of rows with an earlier request date than the row in question.
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


I have tried to tweak it but the result is an empty column K
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
    
        '// MYCHANGES: set appt array
    Set dataRange = ws.Range("H2:H" & maxRow)
    Dim apptarray() As Variant
    apptarray = 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) < apptarray(j, 1)) Then
                count = count + 1
            End If
        Next j
        dataTarget(i, 1) = count
    Next i
End Sub

Open in new window


Hope you are able to help me figure out!! Thank you

Link to my original question
Avatar of Rgonzo1971
Rgonzo1971

Hi,

pls try at line 38
            If (dataSource(j, 1) < dataSource(i, 1) And (dataSource(j, 1) < apptarray(j, 1)) Then

Open in new window

Regards
Avatar of Pearlyn Tan

ASKER

Hi, thanks Rgonzo for you suggestion. However it gives the same result as before (column K is empty).
Could you send a dummy? cannot reach prior question
Hi, this is my prior qn:

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

This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.