Solved

Error msg: Unable to access Worksheet function

Posted on 2002-03-26
9
313 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
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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Office 365 Sync with office 2016 3 40
excel forecast function 1 30
How to export photos individually from a OneNote 2016 39 109
LEFT JOIN Access Query 5 56
In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
My experience with Windows 10 over a one year period and suggestions for smooth operation
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…

910 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

26 Experts available now in Live!

Get 1:1 Help Now