Extract All Numbers from Text and Sum Them in Google Sheets

Published on

Here are two different formula approaches that you can follow to extract all numbers from text and sum them in Google Sheets.

What we want are numbers from text. Even though we want to deal with numbers, we can’t achieve this without using Google Sheets’ text functions.

I will explain in detail the two Google Sheets formulas you can use in your sheet as custom formulas to extract all numbers.

These formulas can extract all sets of numbers from a text irrespective of their position.

Extract All Numbers from Text Using SPLIT and CONCATENATE Functions

For the example, we will consider the following text string in cell A1:

I paid $100.25 for my blog maintenance last month, and this month it's $60.50 as forecasted.

To extract the numbers 100.25 and 60.50 separately in two cells, you can use the formula below:

=SPLIT(A1,CONCATENATE(SPLIT(A1, ".0123456789")))

Explanation:

Syntax: SPLIT(text, delimiter, [split_by_each], [remove_empty_text])

In the inner SPLIT function, which is SPLIT(A1, ".0123456789"):

  • text: A1
  • delimiter: .0123456789

It returns all the text except the numbers in three parts as follows:

I have paid $for my blog maintenance last month and this month it’s $as forecasted

The SPLIT function divides the text string around each character specified in the delimiter as the default split_by_each is TRUE.

We combined this split text and used it as the delimiter in the outer SPLIT. The output will be the numbers in two cells.

This is the first approach to extracting all numbers from a text in Google Sheets. We will see how to sum up these extracted numbers later on.

Extract All Numbers from Text Using SPLIT and REGEXREPLACE Functions

This approach is different. Here, we will use the REGEXREPLACE function to substitute text in the string with | and use that as the delimiter in the SPLIT function.

Here is the formula:

=SPLIT(REGEXREPLACE(A1, "[^\d\.]+", "|"), "|")

The REGEXREPLACE function matches one or more characters that are neither digits nor periods ([^\d.]+) and replaces them with |.

The result will be |100.25|60.50|. The SPLIT function then splits the string at each |.

Summing Extracted Numbers

I am sure all of you can do it now. Simply wrap the above formulas with the SUM function.

Here they are:

=SUM(SPLIT(A1,CONCATENATE(SPLIT(A1, ".0123456789"))))
=SUM(SPLIT(REGEXREPLACE(A1, "[^\d\.]+", "|"), "|"))

Both of these formulas will return 160.75, which is the sum of the extracted numbers.

Follow these approaches to extract multiple sets of numbers from text and sum them in Google Sheets.

Common Errors You May Encounter

I haven’t included error handling above as it might complicate the formulas. Let me address those now since you have a clear understanding of the formulas.

The formulas may return a #VALUE error when the specified cell is empty. In that case, you can use the IFERROR function to remove those errors.

Another issue you may face is when the cell contains only numbers. Here, you may need to use an IF logical test. The following two formulas handle those errors:

=LET(ref, A1, IF(ISNUMBER(ref), ref, IFERROR(SUM(SPLIT(ref&"x", CONCATENATE(SPLIT(ref&"x", ".0123456789")))), 0)))
=LET(ref, A1, IF(ISNUMBER(ref), ref, IFERROR(SUM(SPLIT(REGEXREPLACE(ref, "[^\d.]+", "|"), "|")), 0)))

If the specified cell contains text without numbers, both formulas will return 0.

This way, you can extract numbers from texts, sum them, and handle any errors that may occur.

Resources

Here are some related resources.

Prashanth KV
Prashanth KV
Your Trusted Google Sheets and Excel Guide Prashanth KV brings a wealth of experience in Google Sheets and Excel, cultivated through years of work with multinational corporations in Mumbai and Dubai. As a recognized Google Product Expert in Docs Editors, Prashanth shares his expertise through insightful blogging since 2012. Explore his blog for practical tips and guidance on maximizing your spreadsheet skills.

Filter Top N per Category in Excel (Using FILTER & COUNTIFS)

You can use a combination of FILTER and COUNTIFS to filter the top N...

Rank per Group in Excel

You have two groups of 20 students each. How do you determine the rank...

Real-Time Excel Filtering Using Combo Box & FILTER Function

In this tutorial, you’ll learn how to set up real-time Excel filtering using a...

Google Sheets: Extract Top N per Group from Query Aggregation

When working with grouped and aggregated data in Google Sheets, you might need to...

More like this

Google Sheets: Extract Top N per Group from Query Aggregation

When working with grouped and aggregated data in Google Sheets, you might need to...

How to Extract Top N from Aggregated Query Results in Google Sheets

To summarize data in Google Sheets, you can use the QUERY function. However, if...

How to Use RANK IF in Google Sheets (Conditional Ranking)

You can use the RANK function to rank values in an entire dataset. But...

3 COMMENTS

  1. Hello there,

    Your formula is amazing, but what if the numbers are negative?

    I tested it both formulas and only sum positive. ex. (Car 20, light 10, Amazon -10) result 40 instead of 20.

    Thank you very much for your help.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.