How to Use an Excel IF Formula to Assign Income and Expenses by Cost Code
Question details
The user needs a method to automatically allocate transaction amounts into specific cost-code columns within a cashbook, where income is specifically displayed as a negative value.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Organizing a cashbook that contains columns for incoming money, outgoing money, and cost codes, and needing to distribute these amounts into individual cost-code columns.
- Observed behavior
- Transactions are mapped to their respective cost-code columns using a logical function, with income appropriately formatted as negative amounts.
Ensure your cashbook has clearly defined columns for incoming and outgoing money, and verify that the headers of your destination columns exactly match the cost codes used in your dataset.
Use a Nested IF Formula to Distribute Amounts
Apply a nested IF formula with mixed cell references to check the cost code, pulling the corresponding income or expense amount into the correct column while reversing the sign for income.
This approach uses a logical test to check if the cost code for a transaction matches the header of the column. If it matches, the amount is populated; if not, the cell remains blank. By using mixed absolute references, you can write the formula once and drag it across your entire table.
Make sure that the headers in your target columns (e.g., D1, E1, F1) are identical to the cost codes entered or selected in your data (e.g., Column C).
Select the first cell under your first cost-code column (e.g., D2) and enter the formula: =IF($A2=D$1,-$A2,IF($B2=D$1,$B2,"")). Adjust the column references (A, B) based on where your income and expense data actually sit.
Click the small square at the bottom right corner of the selected cell, then drag it to the right across all cost-code columns, and finally drag it down to apply it to all rows in your cashbook.

Use a PivotTable for Categorized Summaries
For larger datasets, maintain a single column for all transaction amounts and use a PivotTable to summarize income and expenses by cost code without needing complex formulas.
Easily Manage Cashbooks and Formulas with WPS Spreadsheet
WPS Spreadsheet offers powerful formula capabilities and advanced data analysis tools to seamlessly manage your cashbooks. Categorize your income and expenses instantly using the exact same IF formulas and PivotTable features you are already familiar with.
- 1. Open Your Cashbook: Launch WPS Spreadsheet and open your existing cashbook document.
- 2. Apply the Formula: Select the cell under your first cost code header and input the nested IF formula.
- 3. Drag to Autofill: Use the fill handle in the bottom right corner of the cell to drag the formula across your columns and down your rows.
- 4. Create a PivotTable: Alternatively, highlight your raw data, click the 'Insert' tab, and select 'PivotTable' to generate instant financial summaries.

Frequently Asked Questions
Why is my IF formula returning an error instead of a blank cell?
If your formula returns an error like #VALUE!, ensure you have correctly entered the empty quotation marks ("") at the very end of your formula. This tells the spreadsheet to display a blank cell when the cost code does not match.
How do I make income show as a negative amount in the formula?
You can achieve this by placing a minus sign (-) directly in front of the cell reference for your income column within the IF formula, such as -$A2.
Do I need to manually type the formula for every cost code column?
No. By using mixed references—such as $A2 to lock the column and D$1 to lock the row—you can simply drag the fill handle right and down to apply the formula across all cells automatically.
What is the advantage of using a PivotTable over IF formulas for a cashbook?
A PivotTable is often better for larger datasets because it requires less manual formula maintenance, prevents software slow-downs caused by calculating thousands of IF formulas, and can automatically group your data by month or category.




