Using Sumproduct to last row

deduwa

Board Regular
Joined
Jul 28, 2015
Messages
110
Hi,

I am trying to get the sumproduct formula down to last row using vba. The following code returns a compile error.

Dim lastRow As Long
lastRow = Cells(Rows.Count, "A").End(xlUp).Row


Range("AE1").Select
ActiveCell.Formula = "=SUMPRODUCT((S2:S"&lastRow&")&","&(Z2:Z"&lastRow&"))/sum(s2:s"&lastRow&")"

Basically in Cell AE1 the resulting formula should look like the below (Assuming last Row is 150);

=SUMPRODUCT(S2:S150,Z2:Z150)/SUM(S2:S150)

Any help would be appreciated.

Thanks
 
Last edited:

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
It should read:

Code:
Range("AE1").Formula = "=SUMPRODUCT(S2:S" & lastRow & ",Z2:Z" & lastRow & ")/sum(s2:s" & lastRow & ")"
 
Upvote 0
You have extra () and don't need to select the cell

Code:
Sub test()
Dim lr As Long
lr = Cells(Rows.Count, "A").End(xlUp).Row
Range("AE1").Formula = "=SUMPRODUCT(S2:S" & lr & ",Z2:Z" & lr & ")/sum(s2:s" & lr & ")"
End Sub

Note that last row is calculated on A, not S not Z (could get an error)
 
Upvote 0

Forum statistics

Threads
1,216,073
Messages
6,128,645
Members
449,461
Latest member
kokoanutt

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