Vlookup with multiple values and a limiter


New Member
May 7, 2019
Hi guys,

I am trying to use a vlookup up to calculate whether a capital gain or loss has been made or lost when selling shares. A major problem I have is; if shares are purchased in two separate lots and at different prices, when I sell a portion of the shares I need to calculate the capital gain or loss on all the first lot of shares, if I sell more shares than I bought in the first lot then I need to move onto the second lot I bough. Then when I sell the rest of the shares at a later date I need to take off from the amount of shares I have left after selling the first lot.

For example, in the table below, 100 units of ANZ were purchased on 20/5/2016 and a further 60 units of ANZ were purchased on 7/7/2016.
140 units of ANZ were then sold on the 5/8/2016 so the capital gain is worked out by Total return-Total Cost=Capital Gain. However, the Total Cost in this case is 100 units at $10 + 40 units at $2 =$1,080. Therefore, the Capital Gain in this case is $1,680-$1,080=$600. When the further 20 ANZ shares are sold the cost will be the remaining amount of shares (40*$2).

BBOZ sale is similar to ANZ. However, the first sale cost comes from the first purchase and the cost of the second sale of BBOZ comes from 10 units in the first buy then the remaining from the second lot.

Keep in mind that another partial lot of shares could be sold but in this example this doesn't occur.

If you need further explanation please let me know.

Thanks for your help!

Units PurchasedPurchase PriceTotal CostUnits SoldUnit priceTotal Return
Capital Gain


Forum statistics

Latest member

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...