+ Reply to Thread
Results 1 to 14 of 14

Macro for purchasing grouped items calulation

  1. #1
    Registered User
    Join Date
    07-25-2017
    Location
    London
    MS-Off Ver
    2013
    Posts
    8

    Macro for purchasing grouped items calulation

    Hi everyone,
    1.png

    I'm looking for a macro to go through a date sorted table and work out a value for the profit.

    For example, the macro searches from the last row, goes upwards till it hits "Pears". If the "Buy at" column for the first occurence of "Pears"
    is filled, then add that number to the next occurence of a purchase of "Pears" and continue till an occurence of "Pears" has nothing in the "Buy at"
    column but an entry in the "Sell at" column. When there is a value there, then the profit is entered in that row, in green (and a loss in red).
    It is simply the sum of all buys taken subtracted from the sell price.

    This continues upwards through the table. There may be 10 types of fruit listed.

    Any advise on how to accomplish this would be appreciated.

    Thanks in advance!

    James

  2. #2
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Macro for purchasing grouped items calulation

    In your example, I believe pears actually work out to a profit of 96?

    If I understood the requirement correctly:

    Please Login or Register  to view this content.
    Last edited by Arkadi; 07-25-2017 at 08:17 AM.
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  3. #3
    Registered User
    Join Date
    07-25-2017
    Location
    London
    MS-Off Ver
    2013
    Posts
    8

    Re: Macro for purchasing grouped items calulation

    Hi Arkadi.
    The idea is that Pears were bought for £543 on the 13th Jan. Then again for "219 on the 20th Jan. they were sold on the 15th Feb for £315. thus the loss was price sold- total cost or purchase which is £315 -(£219+£543)=-£447 , a loss.
    I'll try your code now.
    Thanks, speak later!
    James

  4. #4
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Macro for purchasing grouped items calulation

    But Pears also have a sell at 543 on row #4...
    Maybe I did not understand well... do we ignore "sell at" until there is no "buy at" value?

  5. #5
    Registered User
    Join Date
    07-25-2017
    Location
    London
    MS-Off Ver
    2013
    Posts
    8

    Re: Macro for purchasing grouped items calulation

    DOH! Sorry I'm an idiot. Didn't see it despite it being in my face. Please ignore the £543 in cell D4. So, total Pear purchases=(£219+£543). They were then sold for £315 on Feb 15th.

  6. #6
    Registered User
    Join Date
    07-25-2017
    Location
    London
    MS-Off Ver
    2013
    Posts
    8

    Re: Macro for purchasing grouped items calulation

    Also, should clarify that the £543 should be deleted, it is an error. So we only add the pears (buys) until a value is encountered in the sell column indicating the end of buying, when a calculation can be made for profit.

  7. #7
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Macro for purchasing grouped items calulation

    Well, if you delete that value, my previous code should work... but to avoid errors, I can just ignore "D" if "C" has a value:

    Please Login or Register  to view this content.
    The code DOES assume that the first instance (when working from the bottom to the top) of any fruit will have a buying price.

  8. #8
    Registered User
    Join Date
    07-25-2017
    Location
    London
    MS-Off Ver
    2013
    Posts
    8

    Re: Macro for purchasing grouped items calulation

    Thanks Arkadi! Does exactly what I wanted to achieve perfectly. I can now grow the spreadsheet as required. So grateful, have a wonderful day!

  9. #9
    Registered User
    Join Date
    07-25-2017
    Location
    London
    MS-Off Ver
    2013
    Posts
    8

    Re: Macro for purchasing grouped items calulation

    Ardaki, small problem (I hope!). I attach a revised image - only "problem seems that if I do another series of buying apples then the total Profit is added to the newly calculated profit (ie the old profit of £2 is added to the new profit of £5, giving £7. Can the new set of apple purchase/sells be reset so it shows profit only from the new buy cycle? 1.png

  10. #10
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Macro for purchasing grouped items calulation

    Yeah, I don't think that is a problem, just didn't know that was a requirement. Give this a go (added one line, which is in red in the code below):

    Please Login or Register  to view this content.
    Last edited by Arkadi; 07-25-2017 at 11:38 AM.

  11. #11
    Registered User
    Join Date
    07-25-2017
    Location
    London
    MS-Off Ver
    2013
    Posts
    8

    Re: Macro for purchasing grouped items calulation

    Strangely, everthing after the 'DIM' part of Dim myDictionary As Scripting.dictionary is highlighted and an error message "User defined Type not defined" is shown in a dialogue box. This did happen with the original code but cleared itself.

  12. #12
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Macro for purchasing grouped items calulation

    my bad... changed it back to Dim ... As Object... it was just for testing that I had it as scripting dictionary. It is a dictionary, but by using dim as object we don't need to add a reference to the library. Sorry about that, I updated my post with code.

  13. #13
    Registered User
    Join Date
    07-25-2017
    Location
    London
    MS-Off Ver
    2013
    Posts
    8

    Re: Macro for purchasing grouped items calulation

    Thanks Arkadi. Had to be away/offline suddenly and wanted to say thanks for your efforts

  14. #14
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Macro for purchasing grouped items calulation

    jamesbarb,

    Thanks for taking the time I'm happy I could help you out. If you are satisfied with the solution, could I ask you to mark the thread as solved? Have a great day, and good luck with the project

+ 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] Show all items from table grouped by name
    By Tr33 in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 04-04-2017, 06:15 AM
  2. [SOLVED] Sum all grouped items from each category
    By Iboughtthefarm.ca in forum Excel General
    Replies: 8
    Last Post: 08-11-2016, 03:37 PM
  3. HELP: Grouped items formula
    By chollo1988 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 05-05-2016, 05:00 AM
  4. % Difference From on Grouped Items
    By mo4391 in forum Excel Charting & Pivots
    Replies: 0
    Last Post: 02-09-2015, 02:36 PM
  5. [SOLVED] Main Totals and Subtotals for grouped items
    By trosasco in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 06-05-2013, 12:12 PM
  6. Total values in MOD grouped items
    By theearthvolta in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-13-2010, 03:06 PM
  7. Show and Hide grouped items
    By malnahar in forum Excel General
    Replies: 0
    Last Post: 09-10-2009, 02:35 PM

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