VBA Count first instance only

Trebor8484

Board Regular
Joined
Oct 27, 2018
Messages
69
Office Version
  1. 2013
Platform
  1. Windows
Hi,

I have a workbook with approximately 500,000 rows which expands each month.

The below code will count each job reference number only once and any subsequent lines for the same reference will return as zero. This code is taking approximately 15 mins to finish the calculation on the column.

Can anyone suggest something that would run quicker please? I have looked into dictionary keys, and wondered if it may be possible to store each unique job reference in memory before outputting the results back to the sheet?

VBA Code:
Sub CountFirstInstance()
    
    Dim sht As Worksheet
    
    Set sht = ThisWorkbook.Sheets("Sheet1")
    
    With sht.Range("Q2:Q" & sht.Cells(Rows.Count, "A").End(xlUp).Row)
        .Formula = "=IF(COUNTIF($O$2:O2,O2)=1,1,0)"
    End With
    
    Do Until Application.CalculationState = xlDone
    Loop
    
    With sht.Range("Q2:Q" & sht.Cells(Rows.Count, "A").End(xlUp).Row)
        .Value = .Value
    End With
    
    'Rest of code
    
End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Another possibility :
• Filter for unique values
• Go to SpecialCells/Visible and enter 1
• Remove filter, go to SpecialCells/Blanks and enter 0.
 
Upvote 0
How about
VBA Code:
Sub Trebor()
   Dim Ary As Variant, Nary As Variant
   Dim r As Long
   
   With ThisWorkbook.Sheets("Sheet1")
      Ary = .Range("O2", .Range("O" & Rows.Count).End(xlUp)).Value2
   End With
   ReDim Nary(1 To UBound(Ary), 1 To 1)
   With CreateObject("scripting.dictionary")
      For r = 1 To UBound(Ary)
         If Not .Exists(Ary(r, 1)) Then
            .Add Ary(r, 1), Nothing
            Nary(r, 1) = 1
         Else
            Nary(r, 1) = 0
         End If
      Next r
   End With
   ThisWorkbook.Sheets("Sheet1").Range("Q2").Resize(r).Value = Nary
End Sub
 
Upvote 0
Solution
Fluff, thanks so much for this. Works perfectly and almost instant.
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0
Hi, appologies for resurrecting one of my old posts. The code above counts the first instance only to replace a countif formula.

How can i adapt the code to provide a running count instead, for example.

Instead of

Excel Formula:
=IF(COUNTIF($O$2:O2,O2)=1,1,0)

Running count

Excel Formula:
=COUNTIF($O$2:O2,O2)

Thanks
 
Upvote 0
Is this what you mean?

VBA Code:
Sub Trebor_v2()
   Dim Ary As Variant
   Dim r As Long
   
   With ThisWorkbook.Sheets("Sheet1")
      Ary = .Range("O2", .Range("O" & Rows.Count).End(xlUp)).Value2
   End With
   With CreateObject("scripting.dictionary")
      For r = 1 To UBound(Ary)
         .Item(Ary(r, 1)) = .Item(Ary(r, 1)) + 1
         Ary(r, 1) = .Item(Ary(r, 1))
      Next r
   End With
   ThisWorkbook.Sheets("Sheet1").Range("Q2").Resize(r).Value = Ary
End Sub
 
Upvote 0
VBA Code:
Sub Trebor()
Dim Ary As Variant, Nary As Variant
Dim r As Long, dic As Object
Set dic = CreateObject("scripting.dictionary")
With ThisWorkbook.Sheets("Sheet1")
    Ary = .Range("O2", .Range("O" & Rows.Count).End(xlUp)).Value2
End With
ReDim Nary(1 To UBound(Ary), 1 To 1)
    For r = 1 To UBound(Ary)
        If Not dic.Exists(Ary(r, 1)) Then
           dic.Add Ary(r, 1), 1
           Nary(r, 1) = 1
         Else
            dic(Ary(r, 1)) = dic(Ary(r, 1)) + 1
            Nary(r, 1) = dic(Ary(r, 1))
         End If
      Next r
ThisWorkbook.Sheets("Sheet1").Range("Q2").Resize(UBound(Nary)).Value = Nary
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,276
Members
449,075
Latest member
staticfluids

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