Formula Errors

How to Use an Excel IF Formula to Assign Income and Expenses by Cost Code

Elise WilliamsElise Williams Nov 10, 2026 868 views

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.

How to Assign Income and Expenses by Cost Code Using Excel IF Formulas
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.
Before you start

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.

Solution 1Recommended

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.

1
Verify Column Headers

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).

2
Enter the IF Formula

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.

3
Fill the Formula Down and Right

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 Nested IF Formula to Distribute Amounts
Understanding Mixed References: The dollar signs ($) in the formula lock specific rows or columns. For example, $A2 locks the column so it doesn't shift when dragged right, while D$1 locks the header row so it stays referenced when dragged down.
Efficient Cashbook Management

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. 1. Open Your Cashbook: Launch WPS Spreadsheet and open your existing cashbook document.
  2. 2. Apply the Formula: Select the cell under your first cost code header and input the nested IF formula.
  3. 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. 4. Create a PivotTable: Alternatively, highlight your raw data, click the 'Insert' tab, and select 'PivotTable' to generate instant financial summaries.
Fully compatible with Microsoft Excel (.xlsx) formats and formulasAdvanced PivotTable features for quick financial summariesIntuitive interface for building and dragging complex nested IF formulasLightweight software that processes large cashbook files quickly
microsoft office alternative - wps office

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.