Solved

Error msg: Unable to access Worksheet function

Posted on 2002-03-26
9
324 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
9 Comments
 
LVL 44

Expert Comment

by:bruintje
ID: 6897404
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
ID: 6897447
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
ID: 6897471
the sring must be formatted like "C4:C14" and tehn this will do

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

HTH:O)Bruintje
0
Create Professional Looking Email Signatures

Create "Professional HTML Email Signatures" with ease.
7 Day Money Back Guarantee if not 100% Satisfied.
Affordable - Try it out for 7 Days Totally Risk Free.
Installers provided for over 45 Email clients.
Both Windows & MAC Supported.
Highly Recommended!

 

Author Comment

by:SBFurr
ID: 6897508
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
 
LVL 44

Expert Comment

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

Expert Comment

by:bruintje
ID: 6897540
or try  Rng = "(" & quart & ")"
0
 
LVL 44

Accepted Solution

by:
bruintje earned 50 total points
ID: 6897591
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
ID: 6897650
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
ID: 6897758
;) looking forward to it
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

My experience with Windows 10 over a one year period and suggestions for smooth operation
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

738 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