RichardMGreen
Well-known Member
- Joined
- Feb 20, 2006
- Messages
- 2,177
Hi all
I've got a worksheet function using SUMPRODUCT that will count all the Mondays in any given month.
It works if I put it on a worksheet quite nicely. The formula is this:-
=SUMPRODUCT(--(WEEKDAY(ROW(INDIRECT(EOMONTH("05/01/2018",-1)+1&":"&EOMONTH("05/01/2018",0))))=2))
You can swap the dates for cell references (which it was originally).
I'm using VBA for something else and I wanted to change the above so I could use Application.WorksheetFunction.SumProduct.... (etc.) but I'm struggling.
I've built the above formula into a variable like this:-
mondays = "=SUMPRODUCT(--(WEEKDAY(ROW(INDIRECT(EOMONTH(""" & start_date & """,-1)+1&" & """:""" & "&EOMONTH(""" & end_date & """,0))))=2))"
The dates are supplied further up the code.
Now for the difficult part.....
I'm using the WorksheetFunction command like this:-
mondaycount = Application.WorksheetFunction.SumProduct(mondays)
but it gives me an error.
I've tried removing the "=SUMPRODUCT(" bit to give me this:-
mondays = "--(WEEKDAY(ROW(INDIRECT(EOMONTH(""" & start_date & """,-1)+1&" & """:""" & "&EOMONTH(""" & end_date & """,0))))=2))"
but I'm still getting an error.
Can anyone let me know where I'm going wrong please?
::edit::
The error message I get (in both cases) is :-
"Unable to get the SumProduct property of the WorkSheetFunction Class"
with a Run-Time error of 1004
I've got a worksheet function using SUMPRODUCT that will count all the Mondays in any given month.
It works if I put it on a worksheet quite nicely. The formula is this:-
=SUMPRODUCT(--(WEEKDAY(ROW(INDIRECT(EOMONTH("05/01/2018",-1)+1&":"&EOMONTH("05/01/2018",0))))=2))
You can swap the dates for cell references (which it was originally).
I'm using VBA for something else and I wanted to change the above so I could use Application.WorksheetFunction.SumProduct.... (etc.) but I'm struggling.
I've built the above formula into a variable like this:-
mondays = "=SUMPRODUCT(--(WEEKDAY(ROW(INDIRECT(EOMONTH(""" & start_date & """,-1)+1&" & """:""" & "&EOMONTH(""" & end_date & """,0))))=2))"
The dates are supplied further up the code.
Now for the difficult part.....
I'm using the WorksheetFunction command like this:-
mondaycount = Application.WorksheetFunction.SumProduct(mondays)
but it gives me an error.
I've tried removing the "=SUMPRODUCT(" bit to give me this:-
mondays = "--(WEEKDAY(ROW(INDIRECT(EOMONTH(""" & start_date & """,-1)+1&" & """:""" & "&EOMONTH(""" & end_date & """,0))))=2))"
but I'm still getting an error.
Can anyone let me know where I'm going wrong please?
::edit::
The error message I get (in both cases) is :-
"Unable to get the SumProduct property of the WorkSheetFunction Class"
with a Run-Time error of 1004
Last edited: