Solved

Error msg: Unable to access Worksheet function

Posted on 2002-03-26
9
310 Views
Last Modified: 2012-06-21
I am writing a function in VBA which needs to find the average of a range of cells.
the range varies, so is passed to my function by another function (which I have tested and works fine).

My function looks like this:

Function AvSqFtbyQ(quart As Integer) As Integer
'calculates the average square footage per quartile
Dim hold As Integer
Dim Rng As String

Rng = GetRangeQ(quart)
hold = Application.WorksheetFunction.Average(Rng)
AvSqFtbyQ = hold

End Function


When I test it in the Immediate Window I get error number 1004: "Unable to get the Average property of the Worksheet Function Class."

am I missing a reference library or do I need to install something?

any ideas as to what is going on here?

thanks, I know I've been asking a lot of questions lately!

Sasha

0
Comment
Question by:SBFurr
  • 6
  • 3
9 Comments
 
LVL 44

Expert Comment

by:bruintje
Comment Utility
Hi Sasha, what does the getrange exactly return? i see a string this modified one works

Function AvSqFtbyQ(quart As Integer) As Integer
'calculates the average square footage per quartile
Dim hold As Integer
Dim Rng As Range

Set Rng = GetRangeQ(quart)
hold = Application.WorksheetFunction.Average(Rng)
AvSqFtbyQ = hold

End Function

presuming your return a range from the getrange function

HTH:O)Bruintje
0
 

Author Comment

by:SBFurr
Comment Utility
actually GetRangeQ returns a string.

I tried just typing in the value in the Immediate window,
debug.pring application.WorksheetFunction.average(C4:C14)
and got the same error #1004.

thanks
Sasha
0
 
LVL 44

Expert Comment

by:bruintje
Comment Utility
the sring must be formatted like "C4:C14" and tehn this will do

hold = Application.WorksheetFunction.Average(Range(Rng))

HTH:O)Bruintje
0
 

Author Comment

by:SBFurr
Comment Utility
one more detail, how do I get the "s around it?
if I tell it
hold = Application.WorksheetFunction.Average(Range(" & Rng & "))
It thinks I'm telling it "& Rng &"

thanks,
Sasha
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 44

Expert Comment

by:bruintje
Comment Utility
didn't it work? with the returned string?
0
 
LVL 44

Expert Comment

by:bruintje
Comment Utility
or try  Rng = "(" & quart & ")"
0
 
LVL 44

Accepted Solution

by:
bruintje earned 50 total points
Comment Utility
allright a bit of rereading

getrange(X) returns a string like C10:C14

then

hold = Application.WorksheetFunction.Average(Range(Rng))

has to work as is without any formatting or does it not?

i presume this is a folloow up on your access question?

:O)Bruintje
0
 

Author Comment

by:SBFurr
Comment Utility
why, yes it is a follow-up to my access question :)
I'm almost done with this project.

hold = Application.WorksheetFunction.Average(Range(Rng))
works, I had mistyped it.

thanks for all your help!

you might see questions on creating graphs in VBA tomorrow...

Sasha
0
 
LVL 44

Expert Comment

by:bruintje
Comment Utility
;) looking forward to it
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In case Office 2010 has not been deployed in your environment, this article may be quite useful. In our office, we wanted a way to deploy Microsoft Office Professional Plus 2010 through an automated batch file via logon script. This article is docum…
Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now