+ Reply to Thread
Results 1 to 9 of 9

Need Summation UDF - (SUMIF)+SUMIF)

  1. #1
    Registered User
    Join Date
    02-10-2013
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    15

    Need Summation UDF - (SUMIF)+SUMIF)

    Hello Friends

    Need help as am new to VBA and plan to use UDF for simplification

    Problem

    Current I have two sheets

    Sheet 1 has Master data with Criteria ranges and Summation columns

    Sheet 2 has data - Excel table range with criteria

    Sheet 1 has master Table

    Criteria1 -ID rank1 Criteria2 -ID rank2
    A 10 I 2
    B 5 II 10
    C 10 III 2


    Sheet 2 has data - Excel table range with criteria

    logName Budget Criteria1 -ID Criteria2 -ID SUM_FORMULA
    AAA 2 A I
    BBB 4 B II
    CCC 2 C III

    I have current formula as Sum_formula = SUMIF(Criteria1 -ID,Criteria1 -ID,rank1)^10+SUMIF(Criteria2 -ID,Criteria2 -ID,rank2)

    Can this formula be done with Simple -UDF.

    regards
    _google

  2. #2
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,926

    Re: Need Summation UDF - (SUMIF)+SUMIF)

    That looks pretty straight-forward to me, why do you want to use a UDF for that?
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  3. #3
    Registered User
    Join Date
    02-10-2013
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Need Summation UDF - (SUMIF)+SUMIF)

    Hi Dibbins,

    Yes , its pretty straight forward but colleague wants simplified formula as they are not used to sumif etc.. that the reason i was look for UDF


    regards
    google

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

    Re: Need Summation UDF - (SUMIF)+SUMIF)

    Is there a specific part of this that you need help with?

    You say that you are new to VBA, so I might suggest that you start with some introductory materials to VBA and UDF's. I put this tutorial together to illustrate the process I use to build UDFs: https://www.excelforum.com/tips-and-...uild-udfs.html

    Once you are comfortable with the basic ideas and mechanics behind creating UDFs, then you will need to become more familiar with the specific statements and programming structure that will be needed for this task. I am not sure what programming structure you will want to use, but some ideas that I expect will become important:
    Loops: http://www.wiseowl.co.uk/blog/s193/vba-loops.htm
    testing conditions (such as whether to include a value in the summation): https://powerspreadsheets.com/if-then-else-statement/
    or maybe simply call the SUMIFS() function within VBA: http://www.cpearson.com/excel/Callin...ionsInVBA.aspx

    That should get you started. Let us know where you get stuck.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  5. #5
    Registered User
    Join Date
    02-10-2013
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Need Summation UDF - (SUMIF)+SUMIF)

    Hi MrShorty,

    After some trial and errors have come with UDF ( honestly lifted some part of the code ). Below is the function which is giving me equivalent result to SUMIF formula combination.

    Is there better method to what i have below, currently way i think .. user will select two criteria columns , corresponding criteria and two sum columns to arrive at the answer.

    Please guide as am having problem with

    1. UDF works fine when sum ranges and criteria ranges and criteria is in the same sheet but

    2. When sum range and criteria range is different sheet it gives #VALUE!

    SUMID(Mas[Like],[@L],Mas[Rank],Mas[act],[@I],Mas[Rank2])


    UDF worked ..........

    Function SUMID(CritRan As Range, crit As Variant, celSum1 As Range, Crit2ran As Range, crit2 As Variant, celSum2 As Range) As Double



    Dim rgCrit As Range, rgSum As Range

    Dim rgCrit2 As Range, rgSum2 As Range

    Dim Cel As Range

    Set Cel = Application.Caller

    Set rgCrit = Range(CritRan, Intersect(CritRan.EntireColumn, Cel.EntireRow))

    Set rgSum = Range(celSum1, Intersect(celSum1.EntireColumn, Cel.EntireRow))

    Set rgCrit2 = Range(Crit2ran, Intersect(Crit2ran.EntireColumn, Cel.EntireRow))

    Set rgSum2 = Range(celSum2, Intersect(celSum2.EntireColumn, Cel.EntireRow))

    SUMID = Application.SumIf(rgCrit, crit, rgSum) ^ 3 + Application.SumIf(rgCrit2, crit2, rgSum2)


    End Function
    Last edited by _google; 06-25-2017 at 03:39 PM.

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

    Re: Need Summation UDF - (SUMIF)+SUMIF)

    Aplogies for the delay, I somehow missed your response from a couple of days ago.

    1. UDF works fine when sum ranges and criteria ranges and criteria is in the same sheet but
    2. When sum range and criteria range is different sheet it gives #VALUE!
    This is usually bad range definitions, and, sure enough, your function contains "unqualified" ranges. For example, you code includes
    Please Login or Register  to view this content.
    Note how I added in italics the ActiveSheet to emphasize that, whenever you fail to qualify a Range reference, Excel is going to assume you mean ActiveSheet.Range(...). This can be especially problematic with UDF's because your code cannot control what sheet is active at the time of execution. An important rule with UDF's (and, IMO, in code in general), never use unqualified Range references. Find some way to explicitly tell VBA where the desired Range is located.

    I think there might be a better way to do those Set Rgcrit=... statements, but I must admit that those structured table references throw me off, because never use structured tables or structured table references. If those were normal A1 references, I could be more help (or if you uploaded a sample file, so I could convert those to regular A1 references).

  7. #7
    Registered User
    Join Date
    02-10-2013
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Need Summation UDF - (SUMIF)+SUMIF)

    Hi MrShorty,

    Have uploaded sample excel file,hope it helps.Please pardon if my earlier explanation was not clear.

    Also if you could suggest an alternative method after correction of my current UDF will help in learning based on analysis of the two versions.

    _google
    Attached Files Attached Files

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

    Re: Need Summation UDF - (SUMIF)+SUMIF)

    As explained in the Intersect method's help file (https://msdn.microsoft.com/VBA/Excel...t-method-excel ), "If one or more ranges from a different worksheet are specified, an error will be returned." If you intend to use ranges from different worksheets in this UDF, then I don't think the Intersect method is going to be usable in this UDF.

    What are you trying to do with the Intersect method? If I copy and adjust so that all the data is on one sheet (and insert a stop statement at the beginning as I explained in my tutorial so I can access VBA's watch and locals window), it appears that the result of the Intersect method is to merely create a copy of the input ranges: rgcrit points to the same range as critran for example. This suggests to me that the Intersect method is either unnecessary to the algorithm, or one of us is not understanding the purpose of the Intersect method.

    I kind of would have expected a simple "one statement" UDF something like this (leaving your old code commented out and including the Stop statement):
    Please Login or Register  to view this content.
    This returns the same result as your FORMULA column indicates when all references are from the same sheet, and works just fine when inputs are from different sheets.

    I note that there seems to be an error in your referencing between the FORMULA column and the UDF column. Your FORMULA column uses a relative reference for the final argument of the second SUMIF() function, yielding the results of 2, 11, and 27. Your UDF column has an absolute reference for the final reference of the second Sumif() function, which yields 2, 10, and 30. I do not know which result is correct, as I cannot tell if you intended for this reference to be relative or absolute. I suggest a couple of moments looking at that to see which is intended, and adjust the desired reference accordingly.

  9. #9
    Registered User
    Join Date
    02-10-2013
    Location
    India
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Need Summation UDF - (SUMIF)+SUMIF)

    Hi MrShorty,

    Thanks for the detailed clarification and Intersect method's help file (https://msdn.microsoft.com/VBA/Excel...t-method-excel ), reference as well. For sure at my end,there is understanding gap of the vba method as specific of Intersect method.

    As mentioned in my initial post am new vba and after google search played with available codes to arrive at UDF .When method is not understood in detail resulting the error messages are also not understood.That the reason took help of the forum gurus, sincerely appreciate your time and effort in explaining the solution.

    "
    I kind of would have expected a simple "one statement" UDF something like this (leaving your old code commented out and including the Stop statement) "

    1. There was referencing error in my example file. Which i did after some deletion of my big file, at my end it is absolute reference.

    2. Solution you indicated work fine and gives me desired result. thanks a lot.


    New thought in my mind now, and will try at my end.

    SUMID = Application.SumIf(CritRan, crit, celSum1) ^ 3 + Application.SumIf(Crit2ran, crit2, celSum2)

    Am thinking if I make CritRan,celSum1,Crit2ran,celSum2 as named ranged so that user does even have to think of selecting range from the master sheet as it is one time activity and sheet remain hidden.

    thanks a lot again for your help and guidance.

    regards

    _google

+ 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. Replies: 2
    Last Post: 06-21-2017, 06:32 PM
  2. SumIf formula or Sumif with pivottable combined question
    By mcgleeuw in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 08-10-2015, 01:05 PM
  3. Replies: 4
    Last Post: 12-04-2014, 02:06 PM
  4. Subtracting from the SUMIF with the difference from the SUMIF range
    By iamblue91 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-04-2014, 09:01 PM
  5. [SOLVED] Shorten SUMIF + SUMIF + SUMIF each with same reference range but different criteria???
    By HeyInKy in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 04-13-2014, 09:25 PM
  6. SUMIF or SUMIF's formula with multiple criteria
    By jackiemariee in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-30-2013, 06:19 PM
  7. Nested SUMIF statement or multiple SUMIF's
    By Dan27 in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 09-17-2009, 06:55 AM

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