Calculation Issues

How to Calculate Stock Profit or Loss on the Sale Row in Excel

Camila MilosovichCamila Milosovich Nov 10, 2026 869 views

Question details

The user needs to calculate the profit or loss of a stock specifically on the row where a sale transaction occurs, accurately matching the stock symbol, purchase quantity, and price across separated rows.

How to Calculate Stock Profit or Loss on the Sale Row in Excel
Product
Excel
Device & OS
not provided
Scenario
Managing a stock transaction log where buy and sell orders for multiple different stock tickers are mixed chronologically, requiring a formula to match corresponding transactions.
Observed behavior
The user is looking for a formula structure that identifies the matching stock symbol and calculates the resulting profit or loss solely on the row designated as a sale.
Before you start

Ensure your transaction data is formatted as an official Excel Table with clear column headers for Date, Stock Symbol, Transaction Type, Units, and Price per Share. This will allow structured references to work properly.

Solution 1Recommended

Using SUMPRODUCT with Structured References

Use a combination of IF and SUMPRODUCT to conditionally calculate profit only on sale rows by matching the stock symbol and date.

This approach assumes you are using an Excel Table format where transactions are logged with positive values for purchases and negative values for sales. The SUMPRODUCT function dynamically finds matching historical transactions for the specific stock ticker.

1
Select the target cell

Click on the first empty cell in your Profit/Loss column within the transaction table.

2
Enter the IF and SUMPRODUCT formula

Type the formula: =IF([@UNITS]>0,0,IF([@SYMB]="","",SUMPRODUCT(([@DATE]>=[DATE])*([@SYMB]=[SYMB])*-[@UNITS]*[$/Share])))

3
Adapt the column headers

Modify [@UNITS], [@SYMB], [@DATE], and [$/Share] to perfectly match the exact column names used in your own spreadsheet.

4
Apply the formula

Press Enter. Since the data is formatted as an Excel Table, the formula will automatically spill or fill down to calculate the profit only on rows where the units indicate a sale.

Using SUMPRODUCT with Structured References
Adjusting the Sign Convention: This specific formula assumes purchases are logged as positive units (>0) and sales as negative units. If your convention is different, adjust the greater-than/less-than operators and the minus sign before [@UNITS] accordingly.

Easily Calculate Stock Portfolio Profits with WPS Spreadsheet

WPS Spreadsheet fully supports advanced array formulas, structured references, SUMPRODUCT, and XLOOKUP, allowing you to seamlessly calculate complex stock profits and losses just like in Microsoft Excel.

  1. 1. Open your transaction log: Launch WPS Spreadsheet and open the file containing your stock transactions.
  2. 2. Format as a Table: Select your data range and press Ctrl+T to format it as a Table, enabling the use of structured references.
  3. 3. Input the formula: In the Profit/Loss column, paste the SUMPRODUCT or XLOOKUP formula designed to calculate the matched transactions.
  4. 4. Auto-fill results: Press Enter. WPS Spreadsheet will automatically calculate and fill the results down the entire column instantly.
Fully compatible with Microsoft Excel formulas including SUMPRODUCT and XLOOKUPBuilt-in financial formatting and templates for portfolio managementLightweight application with extremely fast calculation speedsFree to use across Windows, Mac, iOS, and Android devices
microsoft office alternative - wps office

Frequently Asked Questions

Why does my SUMPRODUCT formula return an error when calculating stock profit?

This usually happens if your data contains text values where numbers are expected (like a currency symbol typed as text), or if your structured reference column headers do not exactly match the actual headers in your Excel Table. Verify there are no typos in the brackets.

Can I calculate stock profit using the FIFO (First-In, First-Out) method in Excel?

Yes, but FIFO requires significantly more complex logic than a standard SUMPRODUCT formula. It typically involves iterative calculations, helper columns, or using Office Scripts/VBA to accurately track which specific shares from which purchase batch were sold.

Is the XLOOKUP function available in older versions of Excel?

No, XLOOKUP is only available in Microsoft 365 and Excel 2021 or newer. If you are using an older version, you must use an INDEX and MATCH array formula combined with multiple criteria to achieve a similar lookup result.

How do I handle partial stock sales with this formula?

The basic SUMPRODUCT formula calculates based on total units matched up to the given date. For partial sales, you need to implement a running total of units (using SUMIFS) to ensure you are only calculating profit on the specific number of shares sold in that exact transaction row, rather than the entire historical batch.