Function Problems

How to Sum Values After the Last Zero in Excel (Formula Guide)

Bushra ParveenBushra Parveen Nov 3, 2026 868 views

Question details

The user needs a dynamic Excel formula to calculate the sum of all values located below the last occurrence of the number zero within a vertical column range, such as G5:G48.

How to Sum Values After the Last Zero in Excel
Product
Excel
Device & OS
not provided
Scenario
Calculating a conditional sum or running total that effectively resets whenever a zero appears in a specific column of data.
Observed behavior
When standard array formulas designed for ranges starting at row 1 (like A1:A48) are copied to other ranges (like G5:G48), they return #REF! errors due to improper relative row offsets.
Before you start

Verify that your data range actually contains at least one zero; if there is a possibility that no zero exists in your data, you will need to wrap your formula in an IFERROR function.

Solution 1Recommended

Use SUM, OFFSET, and LOOKUP to Calculate the Value

This formula dynamically locates the last zero in your specified range and adjusts the calculation boundaries so you do not encounter reference errors.

This approach uses LOOKUP to identify the absolute row number of the last zero. By subtracting the starting row number of your range, the OFFSET function accurately targets the cells strictly below the last zero, regardless of where your data begins.

1
Select the destination cell

Click on the cell where you want the final sum to be displayed.

2
Enter the LOOKUP and OFFSET formula

Type the following formula: `=SUM(OFFSET($G$5,LOOKUP(2,1/($G$5:$G$48=0),ROW($G$5:$G$48))-ROW($G$5)+1,0,ROWS($G$5:$G$48)-LOOKUP(2,1/($G$5:$G$48=0),ROW($G$5:$G$48))+ROW($G$5)-1,1))`

3
Calculate the result

Press Enter. The formula will output the sum of all numbers appearing strictly after the final zero in the range G5:G48.

4
Adjust references for different ranges

If your data is in a different column or row block, ensure you replace both the starting cell (e.g., $G$5) and the entire array references (e.g., $G$5:$G$48) across the entire formula.

Use SUM, OFFSET, and LOOKUP to Calculate the Value
Handling Missing Zeros: If the range may not contain any zeros at all, this formula will return an error. You can add `=IFERROR([formula], SUM($G$5:$G$48))` to sum the entire range instead.
Advanced Formula Solution

Easily Execute Complex Array Formulas with WPS Office

WPS Spreadsheet features a robust calculation engine that handles complex array combinations, OFFSET tracking, and modern functions like XMATCH flawlessly.

  1. 1. Open your dataset: Launch WPS Spreadsheet and open the document containing your target data.
  2. 2. Insert the dynamic formula: Select an empty cell and paste the LOOKUP and OFFSET combination formula.
  3. 3. View instant calculations: Press Enter to execute the formula and instantly see your updated sum.
  4. 4. Debug with ease: Use WPS Office's built-in Error Checking and Evaluate Formula tools if you need to troubleshoot row offsets.
Fully compatible with Microsoft Excel formulas and .xlsx document formats.Process complex, nested array formulas with fast and reliable calculation speeds.Completely free, lightweight, and easy to use across Windows, Mac, and mobile platforms.
QA img-9

Frequently Asked Questions

Why does my OFFSET formula return a #REF! error when I adapt it to a new range?

The #REF! error usually occurs because the row calculations inside the OFFSET function are pushing the reference off the edge of the spreadsheet grid. When your data range (e.g., G5:G48) does not start at row 1, you must subtract the starting row number (e.g., ROW($G$5)) from your LOOKUP calculation to accurately determine the relative row offset.

How does the LOOKUP(2, 1/(range=0), ROW(range)) trick actually work?

This array formula evaluates the data range to find cells equal to 0, which returns TRUE (or 1). Dividing 1 by this array creates an array of 1s and #DIV/0! errors. Looking up the value 2 in this array ignores the errors and finds the last 1 (since 2 is larger than any value present), effectively returning the absolute row number of the last zero.

What happens if there is no zero in my data range?

If no zero is present, the LOOKUP function cannot find a match and will return an #N/A error. To prevent this from breaking your spreadsheet, you can wrap the entire formula in an IFERROR function. For example, using =IFERROR(your_formula, SUM(G5:G48)) will simply sum the entire column if a zero is not found.