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

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
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,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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