+ Reply to Thread
Results 1 to 21 of 21

Summing Array Variable by Another Value in the Array (VBA Code)

  1. #1
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Summing Array Variable by Another Value in the Array (VBA Code)

    I have an array in memory containing user entered data.

    I know the spreadsheet formula is =SUMIF(F$13:F$16,F28, I$13:I$16). How do I write this in VBA Code?

    I need the total of the feevalue by date (see attached spreadsheet). It should remain in memory so I can compare it to a DailyMax amount and adjust the feevalue accordingly.
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Summing Array Variable by Another Value in the Array (VBA Code)

    Here is one example:

    Please Login or Register  to view this content.

  3. #3
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Re: Summing Array Variable by Another Value in the Array (VBA Code)

    Well the problem is that nothing is in the cells yet as the array in entirely in memory at this point.
    I did put the values into cells to write the Sumif() formula within a cell to get the syntax down.

  4. #4
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Summing Array Variable by Another Value in the Array (VBA Code)

    Which array are you trying to sum?

  5. #5
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Re: Summing Array Variable by Another Value in the Array (VBA Code)

    The feevalue(i) to be summed if the iDate(i) are = to each other.

  6. #6
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Summing Array Variable by Another Value in the Array (VBA Code)

    To be honest, I'm not understanding your explanation.
    For what it is worth, here are two functions.
    I have no idea if either is what you need.

    Please Login or Register  to view this content.
    Last edited by StevenM; 05-02-2012 at 06:02 PM.

  7. #7
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Re: Summing Array Variable by Another Value in the Array (VBA Code)

    I apologize for not being more clear.
    I think the latter is the one I am looking for. I will give it a whirl and let you know.

    I appreciate the help.

  8. #8
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Re: Summing Array Variable by Another Value in the Array (VBA Code)

    I am not sure how to use the Function correctly. Also I don't want to have to enter a date. The dates are already in the array.

    Say I enter as pictured it should sum by the date as color coded. The data is entered into an array in memory only.

    Attachment 153830

  9. #9
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Summing Array Variable by Another Value in the Array (VBA Code)

    Somewhere in your code, you would need to add something like:

    Please Login or Register  to view this content.
    Since iDate is an array containing many dates, you would need to tell the function for which dates you would like to sum, yes? I'm assuming that the values in the array feevalue and the dates in the array iDate are coordinated (they were both the same size). The function merely strolls down the elements in the array iDate looking for a date and when it finds that date it then adds the values in freevalue together:

    Please Login or Register  to view this content.
    Note that I'm assuming that for each element (i) in iDate, there is a coordinated value in feevalue. So that feevalue(i) was a fee/charge on iDate(i).

    Thus your image suggests that you need:

    Please Login or Register  to view this content.
    Where dbSumX represents where you want the sum of the fees for each date. Of course, you don't want to hard code the dates like: #6/22/2010#, but somehow you need to tell the function for which dates you want the sum of feevalue, yes?

    Is this making sense, or am I totally off base?

  10. #10
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Re: Summing Array Variable by Another Value in the Array (VBA Code)

    The array consists of the user input revcode(i), hcpc(i), modifier(i), iDate(i), units(i) & charges(i) where i is a user input Integer for line items on a medical bill. The VBA code determines feevalue(i) based on several different Vlookup()s, etc. Then certain rules have to be applied. There is a DailyMax amount (if the hcpc(i) is not exempt). Feevalue(i) cannot exceed that DailyMax for each date. There could be 1 date of service, there could be several but each is contained in the iDate(i) array.

    So I am looking for the function/macro to find each unique date (as there will be multiple feevalue(i) for each date), total the feevalue(i) for each unique date and compare it to the DailyMax amount.

    Instead of coding a date or entering it a second time the code should parse out each unique date from the array, if that's possible.

  11. #11
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Summing Array Variable by Another Value in the Array (VBA Code)

    Please Login or Register  to view this content.
    This code is merely an example of how this might be done. You really don't need: HowManyDifferentDatesInDateArray

    But DatesInDateArray creates a list of dates.

    In you code you could add:

    Please Login or Register  to view this content.
    The string sList will contain a list (separated by a comma) of dates from iDate (one item per date).

    Next:

    Please Login or Register  to view this content.
    This will put each date in a string array.

    Now assuming that for each element (i) in iDate, there is a coordinated value in feevalue.

    Please Login or Register  to view this content.
    For each element (i) in sArray, you can find the sum in feevalue (assuming that each element in feevalue corresponds to an element in iDate).

  12. #12
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Re: Summing Array Variable by Another Value in the Array (VBA Code)

    I added the code for Functions DatesInDateArray & Sumfeevalue4Date at the top.
    I added the other code as follows:
    Please Login or Register  to view this content.
    but it won't compile. It's getting a type mismatch error (array or user defined type expected) on this line:
    Please Login or Register  to view this content.
    Last edited by lloydgodin; 05-04-2012 at 11:41 AM. Reason: clarification

  13. #13
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Summing Array Variable by Another Value in the Array (VBA Code)

    Sorry about that.

    Please Login or Register  to view this content.
    The problem is your iDate is a Variant array, whereas I wrote "DatesInDateArray" as an array of dates.

    If you will go back through my code and change "As Date" to "As Variant" that should correct the problem.

    Such as:

    Please Login or Register  to view this content.

  14. #14
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Re: Summing Array Variable by Another Value in the Array (VBA Code)

    Not your fault and no apologies necessary. I am very grateful for your help.

  15. #15
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Re: Summing Array Variable by Another Value in the Array (VBA Code)

    Wouldn't dbSum need to be an array that corresponds with sList? Otherwise it's only getting the value for the last date in the sList array.

    I think I am executing it in the wrong place too... it should be in a loop(?) defined by number of dates in the array, not how many line items on the bill....

    Please Login or Register  to view this content.

  16. #16
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Summing Array Variable by Another Value in the Array (VBA Code)

    Perhaps something like this:

    Please Login or Register  to view this content.

  17. #17
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Summing Array Variable by Another Value in the Array (VBA Code)

    Wouldn't dbSum need to be an array that corresponds with sList?
    Yes.

    I was thinking that you might be adding these values somewhere in your worksheet. Thus every time you loop you could just add it to your worksheet.

    But if you prefer to have it as an array, then perhaps something like this:

    Please Login or Register  to view this content.

  18. #18
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Re: Summing Array Variable by Another Value in the Array (VBA Code)

    That worked out perfectly...

    I'll probably screw it up when I add the final tweaks...

    Thank you for everything. Not only did you post the code, but you gave me an explanation of what it did to help me learn. I appreciate it.

  19. #19
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Summing Array Variable by Another Value in the Array (VBA Code)

    You're welcome. I'm glad things worked out.

  20. #20
    Registered User
    Join Date
    09-18-2013
    Location
    Brussels, Belgium
    MS-Off Ver
    Excel 2003
    Posts
    1

    Re: Summing Array Variable by Another Value in the Array (VBA Code)

    really really useful post and reply

    Loads of other threads have just advise to sum using loops, but the .Index solution is a LOT more efficient


  21. #21
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Re: Summing Array Variable by Another Value in the Array (VBA Code)

    Gjoka:

    If you have a better solution to the question, feel free to post it along with instructions on why/how the codes work and why you think it's a better option.

    Teach don't troll.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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