Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27
Default Trouble with Average function

Hello all,
Does anyone happen to have any ideas as to why the following code does
not work? It is throwing runtime error 1004, Unable to get the average
property of the worksheet funation class.

lastrow = Sheet2.Cells(Rows.Count, 2).End(xlUp).Row
'myavg = Application.WorksheetFunction.Average("b2:b" & lastrow)
'mymax = Application.WorksheetFunction.max("b2:b" & lastrow)
'mymin = Application.WorksheetFunction.min("b2:b" & lastrow)

I have tried to use the average function on the worksheet and it does
work. The min and the max function also throw the same error.

From what I have read on this site this should be the correct syntax.

Any suggestions would be appreciate.
Thanks in advance,
Terry (stumped) <:)

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,494
Default Trouble with Average function

try these

myavg = Application.WorksheetFunction.Average(Range("b2:b" & lastrow))
mymax = Application.WorksheetFunction.Max(Range("b2:b" & lastrow))
mymin = Application.WorksheetFunction.Min(Range("b2:b" & lastrow))

--


Gary


"Terry K" wrote in message
ups.com...
Hello all,
Does anyone happen to have any ideas as to why the following code does
not work? It is throwing runtime error 1004, Unable to get the average
property of the worksheet funation class.

lastrow = Sheet2.Cells(Rows.Count, 2).End(xlUp).Row
'myavg = Application.WorksheetFunction.Average("b2:b" & lastrow)
'mymax = Application.WorksheetFunction.max("b2:b" & lastrow)
'mymin = Application.WorksheetFunction.min("b2:b" & lastrow)

I have tried to use the average function on the worksheet and it does
work. The min and the max function also throw the same error.

From what I have read on this site this should be the correct syntax.

Any suggestions would be appreciate.
Thanks in advance,
Terry (stumped) <:)



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 225
Default Trouble with Average function

Try
myavg = Application.WorksheetFunction.Average(Range("b2:b" & lastrow))


and similarly for the others.

The problem with your code is that Average etc expect a Range as
argument,
but you've given them a string - more or less the equivalent of
the worksheet function =AVERAGE("B2:B6") instead of
=AVERAGE(B2:B6)

On a worksheet you can use INDIRECT to turn a string to a range,
e.g. =AVERAGE(INDIRECT("B2:B6"))
Or if A1 contains the last row number:
=AVERAGE(INDIRECT("B2:B"&A1))


Hope this helps
Andrew

Terry K wrote:
Hello all,
Does anyone happen to have any ideas as to why the following code does
not work? It is throwing runtime error 1004, Unable to get the average
property of the worksheet funation class.

lastrow = Sheet2.Cells(Rows.Count, 2).End(xlUp).Row
'myavg = Application.WorksheetFunction.Average("b2:b" & lastrow)
'mymax = Application.WorksheetFunction.max("b2:b" & lastrow)
'mymin = Application.WorksheetFunction.min("b2:b" & lastrow)

I have tried to use the average function on the worksheet and it does
work. The min and the max function also throw the same error.

From what I have read on this site this should be the correct syntax.

Any suggestions would be appreciate.
Thanks in advance,
Terry (stumped) <:)


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27
Default Trouble with Average function

Thank you very much, it worked like a charm.
Terry

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Having trouble with function. Not sure what's wrong Excel Worksheet Functions 0 March 28th 07 02:14 AM
Having trouble with function. Teethless mama Excel Worksheet Functions 1 March 28th 07 02:11 AM
Having trouble with AVG Function [email protected] Excel Worksheet Functions 2 August 17th 06 09:19 AM
Error Handling #N/A with AVERAGE Function - Average of values in Row Sam via OfficeKB.com Excel Worksheet Functions 13 July 31st 05 03:59 PM
function trouble hans[_3_] Excel Programming 1 February 7th 05 05:19 PM


All times are GMT +1. The time now is 10:21 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"