All articles

KajoData / Excel

Advanced Excel formulas: calculations, lookups and error checks

A useful formula produces a result you can explain and verify. I use six records to connect calculations, lookups and text processing in a readable worksheet.

Kajo RudzińskiPublished Updated 13 min read

A modular calculating machine with paired blocks, a lookup cabinet and a magnifying lens over its mechanism.

I recognise a useful advanced worksheet by whether its results can be understood and its inputs changed safely. Formula length tells me very little. Five nested functions may solve a real problem, or they may conceal an assumption nobody remembers. I prefer to begin with a small sample, calculate the expected result and then connect the individual operations.

This article uses six synthetic order lines. I will calculate their amounts, select a region, retrieve product categories and split a code with a defined format. Each step includes a situation where valid syntax is insufficient. You can reproduce the complete workflow in a blank worksheet without a company file or any need to guess what the answer should be.

The formulas use English names and commas. The Polish article contains the corresponding local names and semicolons. Separators depend on the application’s and system’s settings. These calculations were executed in Excel for Windows through its English formula interface; the two published versions were compared after mapping function names. That is not a claim to have tested every Excel version and platform.

Prepare the data and record its rules

Enter headers in A1:E1 and the records in A2:E7. Keep text values exactly as shown. Columns D and E must contain numbers. Each row represents an order line, so the same product can appear several times. This distinction matters: repeated product codes are expected in transactions, while repeated codes in the category lookup table will require investigation.

OrderID Product Region Quantity UnitPrice
O01 P-A North 2 50
O02 P-B South 3 20
O03 P-A North 1 50
O04 P-C North 0 80
O05 P-B South -1 20
O06 P-C North 4 80

The negative quantity in O05 represents a one-unit return in this exercise. Zero in O04 is a deliberate zero quantity. All prices use the same fictional value unit. There are no taxes, discounts or exchange rates. I record those simplifications explicitly because multiplying quantity by price cannot decide whether this model fits a real sales process.

Create a separate lookup table in H1:I4. Its headers are Product and Category, followed by P-A / Components, P-B / Accessories and P-C / Equipment. Each code has one category. Reserve column F for line amounts, G for the lookup result and later columns for checks. Keeping inputs, reference data and calculations separate makes an accidentally overwritten formula easier to spot.

Calculate one row before composing functions

Enter Amount in F1. Put this formula in F2 and copy it down to F7. Relative references should change their row numbers, so the next cell uses D3 and E3. Inspect the first and last result rather than assuming that dragging the formula covered the complete dataset. In a larger file, it is easy to stop one record too early.

=D2*E2

The expected amounts are 100, 60, 50, 0, -20 and 320. Their total is 510. North contributes 470 and South contributes 40. You can calculate these numbers by hand before trying a more elaborate formula. I retain the helper column because it makes the row behind a discrepancy visible. A few additional cells can be much easier to understand than one giant expression.

A spare control cell, such as K2, can sum F2:F7. It becomes a reference point for the next calculation. A check is useful only if you understand its dependencies: two formulas using the same incorrect range may agree on an incorrect answer. Alongside the total, I therefore verify that all six lines exist and that their individual amounts match the expected values.

=SUM(F2:F7)

SUMPRODUCT combines multiplication and addition

Put the following formula in K3. It multiplies corresponding elements of the two ranges and adds the products. Our sample should return 510, matching the helper-column total. The SUMPRODUCT documentation explains that array arguments need matching dimensions. D2:D7 and E2:E7 cover the same six order lines in the same order.

=SUMPRODUCT(D2:D7,E2:E7)

I do not replace these ranges with whole-column references. That would make the inputs harder to inspect and expand the function’s work unnecessarily. If the dataset must grow, an Excel table with structured references is worth considering, followed by an explicit new-row check. Here I deliberately retain short fixed ranges so the calculation’s boundaries remain visible in every example.

Pay attention to text in numeric columns. With this separate-argument form, a text entry in an array can be treated as zero. If E2 becomes the text “50”, the result can fall to 410 even though the cell looks similar. Another expression using multiplication inside an argument can behave differently. Do not assume that alternative syntax has identical behaviour before checking the input types.

Add a condition and inspect its scope

I want the amount for North only. The comparison in the first argument produces logical values, and the double unary minus converts them to ones and zeros. South rows receive zero weight. K4 should return 470. The condition changes neither prices nor quantities; it determines which rows participate in this particular total.

=SUMPRODUCT(--(C2:C7="North"),D2:D7,E2:E7)

Replace North with South and check for 40. Then try East, which is absent from the data, and check for zero. This says the condition selected no amounts to add. It does not establish that the business has no activity in that region. The file may simply exclude its transactions. The meaning of an empty selection depends on what the source actually covers.

Also try accidentally shortening one range to D2:D6. The mismatched dimensions should expose an error that needs correction. I do not wrap the whole calculation in error handling that returns zero, because that would turn a broken calculation into a credible-looking number. When adding several conditions, I inspect each condition separately before checking their combined result.

Separate locating a position from retrieving a value

The product code in B2 needs to match the lookup table in H2:H4. In K5, first run MATCH on its own. It should return 1 because P-A occupies the first position in that range. This is not a worksheet row number. The MATCH reference distinguishes relative positions and match modes; the final zero requests an exact match.

=MATCH(B2,$H$2:$H$4,0)

Now connect that position to INDEX in G2 and copy the formula down to G7. INDEX retrieves the value at the selected position in I2:I4. Dollar signs keep the lookup ranges fixed during copying, while B2 remains relative. The expected categories are Components, Accessories, Components, Equipment, Accessories and Equipment. Checking all six reveals more than confirming the first row alone.

=INDEX($I$2:$I$4,MATCH(B2,$H$2:$H$4,0))

This combination lets you select the key and result columns independently. If your version supports XLOOKUP, you can choose the shorter construction discussed in the lookup-function comparison. Both approaches still need a valid lookup table. A newer function does not resolve ambiguous categories or the same product code carrying different meanings in two source systems.

A first match does not prove uniqueness

In K6, count occurrences of the requested product in the reference table. Our codes contain no wildcard characters, so the check has a straightforward interpretation: zero means missing, one is the expected match and more than one needs explanation. Keys containing an asterisk or question mark require additional care over how the criterion is interpreted. The validation rule must fit the identifier format.

=COUNTIF($H$2:$H$4,B2)

In a copy of the exercise, change H4 from P-C to P-A while retaining Equipment as its category. Looking up P-A still returns Components because the first match wins. The separate count now shows two occurrences. You need a decision about the correct key or category. I would not delete the second row merely to make the check pass; I first investigate why the disagreement exists.

Restore H4 to P-C before proceeding. This small habit helps in both exercises and practical work: reverse each deliberate test change or keep it in a separate copy. Otherwise, a later error may come from an earlier experiment. One category per product is an explicit assumption of this example, not a universal property of every product dataset.

A magnifying lens reveals repeated keys beside two drawers containing different objects.

Handle missing codes without losing diagnostics

Replace B2 with P-X. The raw lookup should report that no match exists. A reader-facing version can use IFNA to describe that expected case. Temporarily put the following formula in G2. IFNA handles the unavailable-value error while allowing other error types to remain visible. Restore P-A after completing the experiment so subsequent checks use the original sample.

=IFNA(INDEX($I$2:$I$4,MATCH(B2,$H$2:$H$4,0)),"Missing code")

The message “Missing code” deserves scrutiny too. If a category in the lookup table itself contains an unavailable-value error, the surrounding function can catch that as well. With our clean reference data, the message describes a missing match. In a larger process, I validate the reference table separately. Error handling is a rule for presenting a result, not a substitute for tracing its origin.

I do not replace a missing category with zero because zero is not a product category. Likewise, a broken cell reference does not mean sales were zero. Broad IFERROR handling has legitimate uses, but it requires a deliberate decision about which problems may be hidden and where they can subsequently be inspected. During construction, I run the inner expression without that wrapper first.

Process text according to a known format

Enter CAT - 017 in L2, preserving the ordinary spaces. In M2, remove the redundant spacing. The result should be CAT - 017. TRIM handles particular spaces; it is not a universal cleaner for every invisible character. Text copied from a web page may require identifying a different character before deciding how to replace it.

=TRIM(L2)

In N2, retrieve the prefix before the first hyphen and remove its remaining trailing space. FIND locates the separator, and LEFT takes the preceding characters. The result is CAT. Splitting this work between M2 and N2 keeps the cleaning stage visible. When an unexpected result appears, it is easier to locate the step that needs adjustment.

=TRIM(LEFT(M2,FIND("-",M2)-1))

Change the input to CAT017 and inspect the missing-separator error. Then try CAT-017-PL: the result remains CAT because the rule uses the first hyphen. This is a deliberate rule for a defined code format. I would not transfer it automatically to personal names. The first space is not a dependable boundary between given names, family names or parts of an organisation’s name. Separate source fields are preferable.

Array formulas depend on the application and version

ARRAYFORMULA belongs to Google Sheets, so it should not be entered as an Excel function. Excel has its own array mechanisms. Newer versions support results spilling into neighbouring cells, while older constructions can require Ctrl+Shift+Enter. The array-formula guide provides more detail and examples of the different behaviours.

The formulas shown here return single results and do not require manually typed braces. I do not copy braces from an old worksheet screenshot as a way of repairing a formula. First, I check its function name, language, separator and version support. Evidence that a formula worked in another spreadsheet application does not establish that its syntax is compatible with this one.

When handing over a workbook, I ask the recipient to recalculate a short sample in their environment. I inspect both the displayed answer and the formula. A previously saved value may look correct even when the current application cannot reproduce its calculation. Newer functions and references to external workbooks that the recipient does not have deserve particular attention.

Decide what these numbers should describe

The total of 510 has a clear interpretation in our model: line value after accounting for the recorded return. If someone requested an “average price”, I would ask a further question. Averaging the six prices in column E, averaging the three product prices and dividing value by quantity answer different questions. With zero quantities and returns present, I do not choose a denominator merely because its formula is convenient.

Likewise, our row count is not a customer count. There is no customer identifier or information linking someone’s successive purchases. Dividing the total by six therefore cannot produce a dependable average per customer. Recognising missing information belongs to formula work. Sometimes the appropriate next step is obtaining another column before creating another calculation.

If I only need to total an existing Amount column by region, a conditional-sum function may be the clearest choice. SUMPRODUCT is useful here because it also demonstrates multiplying two ranges. I would not use it automatically for every summary. A readable worksheet uses a construction that fits the task, and its next owner should understand why that construction was selected.

Preserve the relationship between columns

Suppose you sort products alphabetically. Selecting only column B can disconnect a code from its quantity and price if you allow that column to be rearranged independently. Formulas may continue calculating, and the grand total may even remain unchanged. Move complete records and verify that O01 still describes P-A with quantity two and unit price fifty.

I apply a similar check when copying reference data. The key range and category range must retain their shared order. If one range shifts by a row, a correctly located position can retrieve another product’s description. I therefore test several known pairs, including the beginning and end of the range. A uniqueness check addresses a different problem and cannot replace this inspection.

Before a substantial change, I keep a working copy with a recognisable description. Afterwards, I compare amounts, record counts and specific identifiers. Cell colours and the absence of an error message are insufficient evidence. Useful controls should also reveal a worksheet that calculates smoothly while answering a different question from the one intended.

Test changing data before handing over the worksheet

Add a seventh line: O07, P-A, North, quantity 1 and unit price 50. The existing fixed ranges end at row 7, so they exclude this new record in row 8. Notice the omission first, then extend all relevant ranges and copy the row formula. The complete total should become 560 and North should become 520. A successful new-row check confirms more than the original static result.

Return to six lines and separately test a text price, a missing product, a duplicate reference key and a code without its separator. Do not change everything simultaneously. Each experiment should have a name, an expected outcome and a restored starting state. That lets you associate a result with its cause rather than untangling several overlapping modifications.

Finally, record the workbook’s assumptions: the value unit, the meaning of a return, range boundaries and the reference table’s uniqueness rule. Another person should be able to reproduce 510 without asking the author about every detail. Once you can explain those dependencies and detect a deliberately introduced problem, you have a sound basis for extending the formulas to larger datasets.

Key takeaways

Is ARRAYFORMULA an Excel function?

It is a Google Sheets function. Excel has its own array-formula mechanisms, including dynamic arrays in supported versions. Do not transfer the ARRAYFORMULA function name into Excel.

Will INDEX with MATCH detect a duplicate key?

An exact match returns the first matching position. Check key uniqueness separately, for example by counting occurrences in the lookup table.

Should every error become zero?

No. Zero, a missing code and a broken reference mean different things. Handle the expected case while retaining a way to diagnose other errors.

Kajo Rudziński

Newsletter KajoData

Let’s keep in touch.

I write about data analysis, learning and changing careers. My newsletter and the sign-up form are in Polish. If that works for you, I’d be glad to see you there.