VBA SUMPRODUCT formula error

Human_doing

Board Regular
Joined
Feb 16, 2011
Messages
137
Hi all,

I have the below formula in cell N2 of a worksheet, can anyone please tell me how to code VBA to add this to each worksheet in a workbook? In this example there are 53 rows but I am looking for the range to be variable. The first two parts of the formula work in isolation (monthsheet is a list of dates, the two parts search for whether the cell value is between two dates), I think it is the third part where I am looking for a match in column D of value 'C22' that is the problem.

=SUMPRODUCT((E2:E53>MonthSheet!B2)*(E2:E53<MonthSheet!C13)*(D2:D53="C22"))

My attempt is below but it triggers Compile error: Expected: end of statement,

Code:
Sub Looper()

 'Total the number using formula
    Dim Sh As Worksheet
    Dim LR As Long
    For Each Sh In ActiveWorkbook.Worksheets
    With Sh
  
.Range("N2").Formula = "=SUMPRODUCT((E2:E" & LR & ">MonthSheet!B2)*(E2:E" & LR & "<MonthSheet!C13))*(D2:D" & LR & "=C22"))
               
    End With
     
        
Next Sh
 

End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

Forum statistics

Threads
1,224,599
Messages
6,179,828
Members
452,946
Latest member
JoseDavid

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