VBA Sumproduct

helpexcel

Well-known Member
Joined
Oct 21, 2009
Messages
656
Hi,

I was going to write code that put this formula in Columns D to the last Column.

=SUMPRODUCT((Sheet1!$A$3:$A$1000=Sheet2!$A3)+0,(Sheet1!$B$3:$B$1000=Sheet2!A$2)+0,(Sheet1!$C$3:$C$1000)+0)

How do I make this portion dynamic? Sheet1!$A$3:$A$1314
Is there code i can use instead of putting this formula in each row?
 
Last edited:

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Does this help?
Code:
Sub M1()

Dim LR      As Long
Dim x       As Long
Dim sFunc   As String

'Some arbitary random value for last row, minimum 5
LR = Application.Max(Cells(Rows.Count, 1).End(xlUp).Row, 5)

sFunc = "=SUMPRODUCT((Sheet1!$A$3:$A$@LR=Sheet2!$A3)+0,(Sheet1!$B$3:$B$@LR=Sheet2!A$2)+0,(Sheet1!$C$3:$C$@LR)+0)"

For x = 1 To 5
    Cells(x, 1).Formula = Replace(sFunc, "@LR", LR)
Next x

End Sub
 
Last edited:
Upvote 0
It gets the formula right, but it puts the formula in A1 and all the way down depending on x. I need it to start in D and goto to last column/last row.
 
Upvote 0
Try:
Rich (BB code):
Sub M1()

    Dim LR      As Long
    Dim x       As Long
    Dim sFunc   As String

    'Last row in column D (4)
    LR = Cells(Rows.Count, 4).End(xlUp).Row
    
    sFunc = "=SUMPRODUCT((Sheet1!$A$3:$A$@LR=Sheet2!$A3)+0,(Sheet1!$B$3:$B$@LR=Sheet2!A$2)+0,(Sheet1!$C$3:$C$@LR)+0)"
    
    For x = 1 To LR
        Cells(x, 4).Formula = Replace(sFunc, "@LR", LR)
    Next x

End Sub
Change 1 to whatever row the data starts on
 
Last edited:
Upvote 0
How would i adjust this to fill to lastrow and last column, starting M3?

As i'm reading this, think i can just use:

.Range(.Cells(3, 13), .Cells(LR, LastColumn)).Formula = Replace(sFunc, "@LR", LR)
 
Last edited:
Upvote 0
yep works! Do you know if there is a way to do away with the formula altogether, to speed things up?
 
Upvote 0
Probably is but I don't know contents of your sheet, hence what the formula is calculating. Looking purely at the formula, it may be possible to speed up macro by using arrays and dictionaries, or using EVALUATE within the VBA code to output values, but I can't use EVALUATE well enough to suggest.
 
Upvote 0
I have a large worksheet of data on Sheet1 that i am trying to consolidate. I am using SumProduct to look at the columns A,B,C and then place the "price" in the appropriate column on Sheet2.

From this:
[TABLE="width: 510"]
<colgroup><col><col><col><col><col><col></colgroup><tbody>[TR]
[TD]Color[/TD]
[TD]Shape[/TD]
[TD]Code[/TD]
[TD]From[/TD]
[TD]Price[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Blue[/TD]
[TD]Square[/TD]
[TD]12345[/TD]
[TD]Comp A[/TD]
[TD]$100.00[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Blue[/TD]
[TD]Circle[/TD]
[TD]9876[/TD]
[TD]Comp A[/TD]
[TD]$100.00[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Blue[/TD]
[TD]Square[/TD]
[TD]12345[/TD]
[TD]Comp B[/TD]
[TD]$50.00[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Blue[/TD]
[TD]Square[/TD]
[TD]12345[/TD]
[TD]Camp C[/TD]
[TD]$300.00[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]
To this:[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Color[/TD]
[TD]Shape[/TD]
[TD]Code[/TD]
[TD]Comp A[/TD]
[TD]Comp B[/TD]
[TD]Comp C[/TD]
[/TR]
[TR]
[TD]Blue[/TD]
[TD]Square[/TD]
[TD]12345[/TD]
[TD]$100.00[/TD]
[TD]$50.00[/TD]
[TD]$300.00[/TD]
[/TR]
[TR]
[TD]Blue[/TD]
[TD]Circle[/TD]
[TD]9876[/TD]
[TD]$100.00[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
A pivot table can do this for you, why not just use one of those?
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,246
Members
452,623
Latest member
cliftonhandyman

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