Application.WorksheetFunction issue

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
 
Last edited:

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Application.WorksheetFunction.SumProduct requires separate arguments for each part (arrays or ranges) - you can't just pass it a formula string. You could use Evaluate instead, which does expect a formula string.
 
Upvote 0
If you use the macro recorder, select the formula bar and hit enter, you will then see how the formula looks in vba.
Code:
 ActiveCell.FormulaR1C1 = _
        "=SUMPRODUCT(--(WEEKDAY(ROW(INDIRECT(EOMONTH(""05/01/2018"",-1)+1&"":""&EOMONTH(""05/01/2018"",0))))=2))"
Your code looks to have more quotes than necessary.

When I edit the recorded code
Code:
=SUMPRODUCT(--(WEEKDAY(ROW(INDIRECT(EOMONTH(" & start_date & """,-1)+1&"":""&EOMONTH(""" & end_date & """,0))))=2))"
 
Last edited:
Upvote 0
Heres a couple of ways:

Code:
start_date = DateSerial(2018, 1, 5)
For i = Evaluate("EOMONTH(" & CDbl(start_date) & ",-1)+1") To Evaluate("EOMONTH(" & CDbl(start_date) & ",0)")
    If Weekday(i, vbMonday) = 1 Then
        mondays = mondays + 1
    End If
Next

Or maybe:

Code:
start_date = DateSerial(2018, 1, 5)
mondays = Evaluate(Replace("INT((EOMONTH(@,0)-EOMONTH(@,-1)+1+WEEKDAY(EOMONTH(@,-1)+1-2))/7)", "@", CDbl(start_date)))

I couldnt get your other formula to work using evaluate. Not sure why.
 
Upvote 0
Thanks Steve
I ended up doing it the loop way (just slightly differently).

The second way works, but I can't for the life of me see why :confused:
Can you explain please?
 
Upvote 0
It is using this formula:

=INT((start-end+WEEKDAY(start-2))/7)

which is readily available with a bit of searching around. Its essentially getting the number of days between each date and then adding/ subtracting days based on the weekday required. It then divides by 7 (days in a week). Finally it produces the integer part of itself. So 4.1 would become 4 for example.
 
Upvote 0

Forum statistics

Threads
1,223,229
Messages
6,170,881
Members
452,364
Latest member
springate

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