Results 1 to 13 of 13

UDF to calculate average according to condition, ignoring blanks

Threaded View

dmitry UDF to calculate average... 02-11-2011, 05:46 PM
DonkeyOte Re: UDF to calculate average... 02-11-2011, 06:21 PM
romperstomper Re: UDF to calculate average... 02-11-2011, 07:07 PM
Dmitry88 Re: UDF to calculate average... 02-11-2011, 09:22 PM
DonkeyOte Re: UDF to calculate average... 02-12-2011, 04:30 AM
dmitry Re: UDF to calculate average... 02-12-2011, 10:06 AM
DonkeyOte Re: UDF to calculate average... 02-12-2011, 12:48 PM
DonkeyOte Re: UDF to calculate average... 02-12-2011, 03:07 PM
dmitry Re: UDF to calculate average... 02-12-2011, 09:07 PM
DonkeyOte Re: UDF to calculate average... 02-13-2011, 04:38 AM
dmitry Re: UDF to calculate average... 02-14-2011, 12:59 PM
DonkeyOte Re: UDF to calculate average... 02-14-2011, 03:57 PM
dmitry Re: UDF to calculate average... 02-14-2011, 05:51 PM
  1. #1
    Registered User
    Join Date
    02-03-2011
    Location
    VA
    MS-Off Ver
    Excel 2003
    Posts
    17

    UDF to calculate average according to condition, ignoring blanks

    Dear Experts,
    The question is also posted on http://www.ozgrid.com/forum/showthread.php?t=150699, http://www.vbaexpress.com/forum/show...710#post235710, but no feedback so far.

    I have a UDF calculating averages by getting data from subordinate WBS and ignoring blank cells. For instance, if 1.1 is broken down into 1.1.1 (10), 1.1.2 (12) and 1.1.3 (25), the formula finds the corresponding values for the subordinates of 1.1 and calculates the average ((10+12+26)/3 = 16).
    If, however, 2.2 is broken down into 2.1 (50) and 2.2 (blank cell), than the average should be 50.


    When I debug my UDF on, say, cell I40 (5.5, which has 3 subordinates 5.5.1 (82%), 5.5.2 (23%), and 5.5.3 (blank cell, that have a subordinate 5.5.3.1 (85%)), I would expect to have an average of 63.(3)%.
    This is exactly what I get in the Immediate Window (see below) when I get values from the cells.
    ?rgeCriteria.Cells(i, 1).Value 
    5.5.3 
    5.5.2 
    5.5.1 
     
    ?rgeCriteria.Cells(i, 1).Offset(0, 8).Value 
    0.85 
    0.23 
    0.82
    The UDF, however, returns me something different. In part because, I guess, it treats 5.5.3 as blank and does not assume that it may have subordinates. It also does not seem to be drawing results from other cells with the same UDF.
    Please help to fix the UDF. Your help will be very much appreciated. I am attaching the file for your convenience.
    Public Function AVGWBSNB(ByVal rgeCriteria As Range, _ 
        ByVal sCriteria As Range) As Variant 
         
        Application.Volatile (True) 
         
        Dim i As Integer 
        Dim sub_num As Integer 
        Dim blank As Integer 
        Dim sum As Variant 
        Dim count As Integer 
         
        For i = 1 To rgeCriteria.Rows.count 
            If rgeCriteria.Cells(i, 1).Value Like sCriteria.Value & ".#" Then 
                sub_num = sub_num + 1 
                sum = sum + rgeCriteria.Cells(i, 1).Offset(0, 8).Value 
                 
                If IsEmpty(rgeCriteria.Cells(i, 3)) Then 
                    blank = blank + 1 
                End If 
            End If 
        Next i 
         
        If sub_num = 0 Then 
            If IsEmpty(sCriteria.Offset(0, 2)) Then 
                AVGWBSNB = "" 
            Else 
                AVGWBSNB = sCriteria.Offset(0, 2).Value 
            End If 
        Else 
            AVGWBSNB = sum / (sub_num - blank) 
            Exit Function 
        End If 
         
    End Function
    Attached Files Attached Files
    Last edited by dmitry; 02-15-2011 at 09:43 AM.

Thread Information

Users Browsing this Thread

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

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