Faster Method to SUMPRODUCT

breynolds0431

Active Member
Joined
Feb 15, 2013
Messages
303
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Currently, I'm using a =SUMPRODUCT(COUNTIF()) method to build a sequencing of IDs. That is, if there is more than one ID in the range, then a formula assigns a sequence to a column. It takes some time, even when writing the formulas using VBA. Wondering if there's a way to use dictionary or some other memory means of achieving this.

This is what I use to write the formulas to the column. There may be upwards of 100,000 rows that need this formula.

VBA Code:
With Application
     .ScreenUpdating = False
     .Calculation = xlCalculationManual
End With
        ws.Range("B13:B" & Range("D" & rows.count).End(xlUp).row).Formula = _
            "=SUMPRODUCT(COUNTIF($D$13:D13,D13))"
With Application
     .ScreenUpdating = True
     .Calculation = xlCalculationAutomatic
End With
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
If you just need a number to differentiate between duplicate IDs in a different column =ROW() works and is faster than your SUMPRODUCT.
 
Upvote 0
The formula, or sequence column, would need to create a sequential numbering of the duplicated ID. Does =ROW() somehow do that with maybe an IF statement?

rand.xlsx
AB
1IDSequence
2DV880442UE1
3KP905285NJ1
4HR556652HO1
5HR556652HO2
6LZ855837OU1
7TH666004HS1
8YB748788CY1
9VJ680184JV1
10VJ680184JV2
11VJ680184JV3
12VF962409SU1
13HE229935IB1
14HR556652HO3
Sheet1
Cell Formulas
RangeFormula
B2:B14B2=SUMPRODUCT(COUNTIF($A$2:A2,A2))
 
Upvote 0
Why are you using SumProduct?

Countif or Countifs may suffice.

T202301a.xlsm
AB
1IDSequence
2DV880442UE1
3KP905285NJ1
4HR556652HO1
5HR556652HO2
6LZ855837OU1
7TH666004HS1
8YB748788CY1
9VJ680184JV1
10VJ680184JV2
11VJ680184JV3
12VF962409SU1
13HE229935IB1
14HR556652HO3
2d
Cell Formulas
RangeFormula
B2:B14B2=COUNTIFS($A$2:A2,A2)
 
Upvote 0
Why are you using SumProduct?

Countif or Countifs may suffice.

T202301a.xlsm
AB
1IDSequence
2DV880442UE1
3KP905285NJ1
4HR556652HO1
5HR556652HO2
6LZ855837OU1
7TH666004HS1
8YB748788CY1
9VJ680184JV1
10VJ680184JV2
11VJ680184JV3
12VF962409SU1
13HE229935IB1
14HR556652HO3
2d
Cell Formulas
RangeFormula
B2:B14B2=COUNTIFS($A$2:A2,A2)

You're right. SUMPRODUCT doesn't appear to be needed. Not sure where that came from. I removed it but it unfortunately doesn't offer any quicker processing.
 
Upvote 0
You can try Countifs; it may be faster.

You could post a concise example of what you are trying to achieve; see the forum's tool XL2BB.
There are new functions in 365 that may help achieve your ultimate result.
 
Upvote 0
Appreciate the feedback. Have tried =COUNTIFS, but there's no change in processing. A concise example would be 100,000 rows of the same data that I already posted. I don't think 100,000 rows of XL2BB in this post would be ideal. And this particular workbook would be run in 2016.

The idea would be for the formulas to not take ~60 seconds to complete. I believe the Cells Fill is what takes so long (both by manually autocompleting and by using the Range.Formula = I provided in post #1). So an alternative that would process this in memory would be great if anyone knows of a way.
 
Upvote 0
A VBA dictionary solution won't update when you add and delete records but if it is still of interest try this:

VBA Code:
Sub Add_SeqNo()

    Dim ws As Worksheet
    Dim rng As Range
    Dim arr As Variant
    Dim lastRow As Long, i As Long, iseq As Long
    Dim dict As Object, dictKey As String
    
    
    Set ws = ActiveSheet
    With ws
        lastRow = .Range("A" & Rows.Count).End(xlUp).Row
        Set rng = .Range("A2:B" & lastRow)
        arr = rng.Value2
    End With
    
    Set dict = CreateObject("Scripting.dictionary")
    ' Load details into Dictionary and add sequence no
    For i = 1 To UBound(arr)
        dictKey = arr(i, 1)
        If Not dict.exists(dictKey) Then
            iseq = 1
            dict(dictKey) = iseq
        Else
            iseq = dict(dictKey) + 1
            dict(dictKey) = iseq
        End If
        
        arr(i, 2) = dict(dictKey)
    Next i
    
    rng.Columns(2).Value2 = Application.Index(arr, 0, 2)
End Sub
 
Upvote 0
Solution
A VBA dictionary solution won't update when you add and delete records but if it is still of interest try this:

VBA Code:
Sub Add_SeqNo()

    Dim ws As Worksheet
    Dim rng As Range
    Dim arr As Variant
    Dim lastRow As Long, i As Long, iseq As Long
    Dim dict As Object, dictKey As String
   
   
    Set ws = ActiveSheet
    With ws
        lastRow = .Range("A" & Rows.Count).End(xlUp).Row
        Set rng = .Range("A2:B" & lastRow)
        arr = rng.Value2
    End With
   
    Set dict = CreateObject("Scripting.dictionary")
    ' Load details into Dictionary and add sequence no
    For i = 1 To UBound(arr)
        dictKey = arr(i, 1)
        If Not dict.exists(dictKey) Then
            iseq = 1
            dict(dictKey) = iseq
        Else
            iseq = dict(dictKey) + 1
            dict(dictKey) = iseq
        End If
       
        arr(i, 2) = dict(dictKey)
    Next i
   
    rng.Columns(2).Value2 = Application.Index(arr, 0, 2)
End Sub
That works, thanks! The data would either have new data appended to it or be replaced entirely, which in time I could have the sequencing updated entirely over again.
 
Upvote 0

Forum statistics

Threads
1,214,987
Messages
6,122,618
Members
449,092
Latest member
amyap

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