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

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
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,224,824
Messages
6,181,186
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