+ Reply to Thread
Results 1 to 5 of 5

worksheetfunction.average

  1. #1
    Registered User
    Join Date
    07-11-2017
    Location
    Belgium
    MS-Off Ver
    2007
    Posts
    2

    Smile worksheetfunction.average

    Hi,

    I have next issue with this code

    Please Login or Register  to view this content.
    It calculates avgtime correct but when it starts with the next line the program returns error 1004
    The only difference is that t is an array with the same time values as above.
    Somebody can help me to solve this probem?

    Thanks in advance
    Please Login or Register  to view this content.
    Last edited by tifr; 07-11-2017 at 06:00 PM.

  2. #2
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: worksheetfunction.average

    Hello tifr,

    Welcome to the Forum,

    Moderation removed. Code Tags added.
    Last edited by Winon; 07-12-2017 at 07:09 PM.
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  3. #3
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: worksheetfunction.average

    I don't know for sure why this is working this way, but here's my guess -- Excel's average function is designed to ignore any "non-numeric" data. The way you have t defined, t is an array of variant/strings. When you reference each element separately, VBA decides to convert the individual elements to a suitable numeric type for the AVERAGE() function -- meaning converting the strings to dates to doubles. When you reference the array, then the array is passed to the AVERAGE() function as an array of strings, and the average function ignores the strings. The error comes when the AVERAGE() function cannot find any numbers in the array, and it can't divide by n=0.

    I used this version of your sub to test further
    Please Login or Register  to view this content.
    Note that I have three different ways to define t. The first one is the one you started with -- t is an array of variant/strings. The second one is an array of variant/dates. The last one is an array of variant/doubles. All three variations work with the first average function (referencing individual elements of the array), but only the last works for the second average function (referencing t as an array of variant/double). Additional testing suggests that it would also work with other numeric data types (like integer, decimal).

    It appears to me to be an issue with data types. I don't know how to further test/debug this without knowing a lot more (maybe the proprietary stuff that MS is not going to tell us) about the internal workings of the AVERAGE() function and how it decides which data types to include and which to ignore. However, the end solution seems relatively simple -- using suitable type conversion functions (https://msdn.microsoft.com/en-us/vba...sion-functions ) to coerce the elements of t into numeric data types that the AVERAGE() function will recognize as numbers.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  4. #4
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: worksheetfunction.average

    Only this one worked:

    Please Login or Register  to view this content.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  5. #5
    Registered User
    Join Date
    07-11-2017
    Location
    Belgium
    MS-Off Ver
    2007
    Posts
    2

    Re: worksheetfunction.average

    Thanks for the solution.
    Another way is to copy the array with timestamps to a workheet and make a range.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] worksheetfunction.average
    By Sc0tt1e in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-27-2017, 07:55 PM
  2. [SOLVED] Unable to get the average property of the worksheetfunction class
    By Lukael in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 09-01-2015, 01:41 AM
  3. [SOLVED] WorksheetFunction Average with variable ranges, help needed
    By MichaelMcF in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-26-2014, 03:43 PM
  4. use of WorksheetFunction.max, min and average
    By obedus in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-08-2014, 07:51 AM
  5. VBA Worksheetfunction
    By TrueTears in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-27-2013, 12:21 PM
  6. [SOLVED] Using a filename in worksheetFunction.Average(Range("range").offset(0.0)
    By MikeSD in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-12-2012, 12:06 PM
  7. WorksheetFunction in VBA
    By MacroLide in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-20-2009, 06:01 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1