+ Reply to Thread
Results 1 to 6 of 6

Create Custom Sum Function

  1. #1
    Registered User
    Join Date
    05-08-2005
    Posts
    12

    Create Custom Sum Function

    I would like to create a custom Sum function using VB.

    If would Sum two ranges numerator and denominator and then divide N/D.
    It would only add values to the Num and Den if they were positive in both columns eg.

    Num Den
    5 10 Include
    0 5 Don`t Include
    10 2 Include
    5 -5 Don`t include

    So the Num = 15 and Den = 12 so output would be 15/12 = 1.25

    So far I have ...
    Function Create_aggregate(Numerator As Range, Demoninator As Range)
    Dim SumN As Integer
    Dim DumD As Integer
    For Each i In Numerator
    For Each j In Denominator
    If i.Value > 0 Then SumN = SumN + i
    If j.Value > 0 Then SumD = SumD + j
    Else: SumN = SumN - i
    Next i
    Next j
    Create_aggregate = SumN / SumD
    End Function

    This is effectively the sumif formula which works for one for each but not for two .. Thanks for suggestions

  2. #2
    Valued Forum Contributor
    Join Date
    07-11-2004
    Posts
    851
    how about this? - but I am no pro at vba

    Dim SumN As Integer
    Dim DumD As Integer
    sumd = 0
    sumn = 0
    For Each i In Numerator
    For Each j In Denominator
    If i.Value <= 0 Then goto nexti
    If j.Value <= 0 Then goto nexj
    SumD = SumD + j
    SumN = SumN + i
    nexti:
    Next i
    nextj:
    Next j
    not a professional, just trying to assist.....

  3. #3
    Registered User
    Join Date
    05-08-2005
    Posts
    12

    Can you use 2 for each statements?

    Thanks, but I think the problem is with the two For Each statements. Can you use 2 for each statements?

  4. #4
    Registered User
    Join Date
    09-22-2003
    Posts
    9
    Hi,
    instead of double For Each cycles you have to use something like following:

    ...
    For k = 1 to Numerator.Count 'number of cells in range Numerator
    If Numerator(k) > 0 and Denominator(k) > 0 Then
    sumN = sumN + Numerator(k)
    sumD = sumD + Denominator(k)
    End If
    Next k
    ...


    Jarek

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello L2B,

    Here is way to simplify your code by letting Excel do the work for you. Just substitute your actual range for "Num" and "Den".

    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

  6. #6
    Registered User
    Join Date
    09-22-2003
    Posts
    9
    Leith,
    I think L2B wants BOTH of adjacent elements have to be positive.
    Another way is using formula, not VBA, for example (Nominator in A1:A4,
    Denominator in B1:B4):

    =SUMPRODUCT((A1:A4>0)*(B1:B4>0)*(A1:A4))/SUMPRODUCT((A1:A4>0)*(B1:B4>0)*(B1:B4))

    Jarek

+ Reply to Thread

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