+ Reply to Thread
Results 1 to 2 of 2

Errors with User Defined Function VBA

  1. #1
    Registered User
    Join Date
    02-01-2021
    Location
    Birmingham
    MS-Off Ver
    2016
    Posts
    27

    Errors with User Defined Function VBA

    Hi everyone,
    I have two UDFs designed to calculate averages based of the values offset (0, -1) and (0,-2) to any cells in my given range where the date year = year specified in the function. This is to work out yearly averages. These UDFs have been named AnnAv and AnnAv2 respectively.
    I have attached a sample workbook to demonstrate where errors crop up:

    Sheet 1:
    All calculations work. Test1 has scores and dates recorded and uses AnnAv to find the yearly averages; Test2 has scores ratings and dates recorded, and uses AnnAv2 to find the yearly averages.

    Sheet 2:
    The introduction of blank spaces for future input creates a #VALUE! error in the outcome of AnnAv2 (bottom table) but works fine for AnnAv (top table).

    Sheet 3:
    In both cases, averages are being calculated for ratings which are all offset (0, -1) from the date columns, so only AnnAv is used. In the bottom table, the inclusion of words in the range seems to be the cause of the #VALUE! error.

    Can figure out why these errors are occurring?
    Attached Files Attached Files
    Last edited by lilybickel; 02-10-2021 at 07:18 AM. Reason: solved

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

    Re: Errors with User Defined Function VBA

    The first thing I did was choose one of the functions and add a stop statement to the top. Then it would enter debug mode (for each copy of the function. I ended up deleting most references to the functions so that I didn't need to wade through all of the instances of the function.) While in debug mode, I could step through the function and found that it errored (without warning or message) on:

    Please Login or Register  to view this content.
    What this statement is doing is checking if Ccell contains a date and if the year of that date is the same as yr. When Ccell.value is a number or a string that can be interpreted as a date (or empty where the value is interpreted to be the number 0), these tests work just fine. When Ccell.value is a text string that cannot be interpreted as a date, the Year() function errors.

    What I think you will need to do is separate these into separate, nested If..Then statements.
    Please Login or Register  to view this content.
    This way, when Ccell is not a date formatted cell, then it can skip over the Year() function and avoid the runtime error.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

+ 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] Usage of standard functions into custom function (or user defined function)
    By tusharb in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-13-2016, 12:43 AM
  2. User-Defined Function Auto-Completes but gives #NAME? Errors
    By jjouett in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-18-2016, 07:46 AM
  3. COUNTIF/COUNTIFS function + nested user-defined function
    By shamjamali in forum Excel General
    Replies: 1
    Last Post: 05-12-2015, 09:12 PM
  4. [SOLVED] User defined function returns an error on a standard function used in it.
    By pb48 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-23-2013, 01:35 PM
  5. User Defined Function for #DIV/0! errors
    By zoudazou in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-15-2012, 05:15 PM
  6. [SOLVED] Excel - User Defined Function Error: This function takes no argume
    By BruceInCalgary in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-18-2006, 04:05 PM
  7. Replies: 0
    Last Post: 06-20-2006, 10:55 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