Tuesday 10 July 2012

Runtime error 1004 : Unable to get the Average property of the WorksheetFunction class


Runtime error 1004 : Unable to get the Average property of the WorksheetFunction class

This is an easy one. I was getting a runtime error 1004 when I was using WorksheetFunctions on Excel 2007 with certain spreadsheets/workbooks on Sharepoint.


The code:

'Put the average into a variable
'yearRange is a string containing a range address like "E77:BI77"
calculatedAvg = Application.WorksheetFunction.Average(Sheets("Supply").Range(yearRange))

As it turns out the problem wasn't with the code or syntax, but the contains of the target range. If there are any non-numeric items in there (like text, #N/A, #DIV/0, or a "" from an IF statement in my case) it won't work and throws an error.

I changed my IF statements to return 0s instead of "" and now it works!

No comments:

Post a Comment