VBA SUMPRODUCT with variable range

Human_doing

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

I have the following formula in cell K10 of a worksheet which lets me know how many cells within range E2:311 fall between the dates in cells K2 and K3. However, there will not always be 110 rows below the header row, can anyone please tell me how I could use VBA to calculate the final row with data and add add the appropriate cell reference in place of E111?

=SUMPRODUCT((E2:E111>K2)*(E2:E111 < K3))

Many thanks
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Her you are:

Code:
Sub Test()
    Dim Sh As Worksheet
    Dim LR As Long
    Set Sh = Worksheets("Sheet1")
    With Sh
        LR = .Range("E" & .Rows.Count).End(xlUp).Row
        .Range("K10").Formula = "=SUMPRODUCT((E2:E" & LR & ">K2)*(E2:E" & LR & "<K3))"
    End With
End Sub
 
Upvote 0
Hi, thanks that is of great help. I wonder if I could ask one more thing - this formula will actually be placed on a huge number of worksheets, could you please tell me how I could create a 'loop' that drops this formula in cell I2 of each worksheet in the workbook?

Thanks
 
Upvote 0
Try:

Code:
Sub Test()
    Dim Sh As Worksheet
    Dim LR As Long
    For Each Sh In ActiveWorkbook.Worksheets
        With Sh
            LR = .Range("E" & .Rows.Count).End(xlUp).Row
            .Range("I2").Formula = "=SUMPRODUCT((E2:E" & LR & ">K2)*(E2:E" & LR & "<K3))"
        End With
    Next Sh
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,285
Members
452,902
Latest member
Knuddeluff

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