How to Calculate Stock Profit or Loss on the Sale Row in Excel
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.

- 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.
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.
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.
Click on the first empty cell in your Profit/Loss column within the transaction table.
Type the formula: =IF([@UNITS]>0,0,IF([@SYMB]="","",SUMPRODUCT(([@DATE]>=[DATE])*([@SYMB]=[SYMB])*-[@UNITS]*[$/Share])))
Modify [@UNITS], [@SYMB], [@DATE], and [$/Share] to perfectly match the exact column names used in your own spreadsheet.
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 XLOOKUP to Match Buy and Sell Rows
If you have distinct, 1-to-1 buy and sell rows and use a modern version of Excel, XLOOKUP can find the corresponding purchase transaction to calculate the difference.
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. Open your transaction log: Launch WPS Spreadsheet and open the file containing your stock transactions.
- 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. Input the formula: In the Profit/Loss column, paste the SUMPRODUCT or XLOOKUP formula designed to calculate the matched transactions.
- 4. Auto-fill results: Press Enter. WPS Spreadsheet will automatically calculate and fill the results down the entire column instantly.

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.




