Instead formula SUMPRODUCT, I need a VBA solution

motilulla

Well-known Member
Joined
Feb 13, 2008
Messages
2,422
Office Version
  1. 2010
Hello,

I am using the following SUMPRODUCT formula in the column N in the cell N6 to down

=SUMPRODUCT(--($D$6:$D$4108=J6)*--($E$6:$E$4108=K6)*--($F$6:$F$4108=L6))

As sample data it is shown only result for 10 rows J6:L15

n1n2n3n4n5n1n2n3SUMAPRODUCTO
41224273614102
14101923110480
141528354016134
122021454849210
110121648446480
3812294359210
12373940502738480
10284047481047480
372545501415350
691525381846480
59192138
57142049
28173250
1415253547
59202126
1020222431
49152147
527314042
16133049
1324264749
1823374648
1114242529
37122634
110173348
56113044
815263048
1022273848
717203550
110444548
1025414748
68273741
1213172243
519314350
16132228
2021273340
39203042
1015174045
1013203341
16131726
110293848
58142232
14104145
712273848
1525264041
1828394648
923294149
16131516
525323743
318222732
49142127
48111946
412254648
219242649
45394648

But my really data have 35000 rows so when I do formula copy from N6 till down to N35005 it freeze my computer

I tried to use it in the VBA as below but it does not work as it should

VBA Code:
Sub Test_SUMPRODUCT()
    With Range("N6:N35005")
        .Formula = "=SUMPRODUCT(--($D$6:$D$59=J6)*--($E$6:$E$59=K6)*--($F$6:$F$59=L6))"
        .Value = .Value
    End With
End Sub

So far I need help can it is possible this could be done via VBA code, which can do the same job and leave the SUMPRODUCT values only

Thank you all.

I am using Excel 2000

Regards,
Moti
 

Attachments

  • VBA SUMPRODUCT.png
    VBA SUMPRODUCT.png
    53.6 KB · Views: 17

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
With the function EVALUATE you can retrieve the value of a formule.
 
Upvote 0
After several trials I got good results using this code:
Code:
Sub SumProd()
Dim wOne, wTwo, oArr() As Long, myTim As Single
Dim I As Long, J As Long, K As Long, L As Long
Dim myDic As Object, myK As String, Result As String
'
Result = "P6"                   '<<< The starting position for results
Sheets("mySheet").Select        '<<< The sheet with the data
'
wOne = Range(Range("D6"), Range("D6").End(xlDown)).Resize(, 3).Value
wTwo = Range(Range("J6"), Range("J6").End(xlDown)).Resize(, 3).Value
ReDim oArr(1 To UBound(wTwo), 1 To 1)
myTim = Timer
Set myDic = CreateObject("Scripting.Dictionary")
For I = 1 To UBound(wOne)
    myK = wOne(I, 1) & "-" & wOne(I, 2) & "-" & wOne(I, 3)
    If myDic.Exists(myK) Then
        myDic.Item(myK) = myDic.Item(myK) + 1
    Else
        myDic.Add (myK), 1
    End If
Next I
For K = 1 To UBound(wTwo)
    myK = wTwo(K, 1) & "-" & wTwo(K, 2) & "-" & wTwo(K, 3)
    If myDic.Exists(myK) Then
        oArr(K, 1) = myDic.Item(myK)
    End If
Next K
Debug.Print Format(Timer - myTim, "0.00")
Range(Result).Resize(UBound(oArr) + 100, 1).ClearContents
Range(Result).Resize(UBound(oArr), 1) = oArr
Set myDic = Nothing
End Sub

The lines marked <<< needs to be customized according to the comment.

I assumed that the data are in D6:Hxxxx (the first block) and in J6:Lyyyy (the second block)

Put the code into a standard vbba module of your vba project, then run Sub SumProd

Try…
 
Upvote 0
Solution
After several trials I got good results using this code:
Code:
Sub SumProd()
Dim wOne, wTwo, oArr() As Long, myTim As Single
Dim I As Long, J As Long, K As Long, L As Long
Dim myDic As Object, myK As String, Result As String
'
Result = "P6"                   '<<< The starting position for results
Sheets("mySheet").Select        '<<< The sheet with the data
'
wOne = Range(Range("D6"), Range("D6").End(xlDown)).Resize(, 3).Value
wTwo = Range(Range("J6"), Range("J6").End(xlDown)).Resize(, 3).Value
ReDim oArr(1 To UBound(wTwo), 1 To 1)
myTim = Timer
Set myDic = CreateObject("Scripting.Dictionary")
For I = 1 To UBound(wOne)
    myK = wOne(I, 1) & "-" & wOne(I, 2) & "-" & wOne(I, 3)
    If myDic.Exists(myK) Then
        myDic.Item(myK) = myDic.Item(myK) + 1
    Else
        myDic.Add (myK), 1
    End If
Next I
For K = 1 To UBound(wTwo)
    myK = wTwo(K, 1) & "-" & wTwo(K, 2) & "-" & wTwo(K, 3)
    If myDic.Exists(myK) Then
        oArr(K, 1) = myDic.Item(myK)
    End If
Next K
Debug.Print Format(Timer - myTim, "0.00")
Range(Result).Resize(UBound(oArr) + 100, 1).ClearContents
Range(Result).Resize(UBound(oArr), 1) = oArr
Set myDic = Nothing
End Sub

The lines marked <<< needs to be customized according to the comment.

I assumed that the data are in D6:Hxxxx (the first block) and in J6:Lyyyy (the second block)

Put the code into a standard vbba module of your vba project, then run Sub SumProd

Try…
Anthony47, speechless VBA worked 100% perfect! And I got the results in a less than 1 second.

I do appreciate your help and time you spent to solve this as per request.

This is a second request please could you guide how can I change the data columns instead as now using D6+E6+F6, I want to use D6+F6+G6 (and the results columns will be remain the same)

Kind Regards
Moti :)
 
Upvote 0
could you guide how can I change the data columns instead as now using D6+E6+F6, I want to use D6+F6+G6

Modify these two lines:
VBA Code:
wOne = Range(Range("D6"), Range("D6").End(xlDown)).Resize(, 4).Value    'it now is Resize(,3)

Code:
myK = wOne(I, 1) & "-" & wOne(I, 3) & "-" & wOne(I, 4)                 'It is now I,2 and I,3

Bye
 
Upvote 0
Modify these two lines:
VBA Code:
wOne = Range(Range("D6"), Range("D6").End(xlDown)).Resize(, 4).Value    'it now is Resize(,3)

Code:
myK = wOne(I, 1) & "-" & wOne(I, 3) & "-" & wOne(I, 4)                 'It is now I,2 and I,3

Bye
Anthony47, much kind of you fulfilling my both request, it is working like magic thank you for you time solving my requests.

Good Luck and nice start of the week ahead.

Kind Regards
Moti :)
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,190
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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