Ravindra BagaleCourses & study guides मराठी Track your progress

Split Quantity and Unit in Excel (2 kg, 500 gram) with TEXTBEFORE, TEXTAFTER and NUMBERVALUE: Convert Grams to Kg

The custom number format chapter showed how to avoid typing "2 kg" into a cell. But often the data arrives that way: an order export with quantities like "2 kg", "1.5 kg" and "500 gram", and the boss wants the bill amount. In this chapter you split each quantity into its number and its unit with TEXTBEFORE and TEXTAFTER, make the number a real number with NUMBERVALUE, convert grams to kg with IF, and work out the amount.

What you'll learn in this chapter

  • Why SUM of "2 kg", "500 gram" returns 0, and ISNUMBER
  • TEXTBEFORE: the part before the space
  • NUMBERVALUE: text "2" to the number 2
  • TEXTAFTER: the unit after the space
  • IF: grams to kg, then amount = kg × price
  • Nesting in one formula, and the no-space trap
  • The same with LEFT, MID and FIND for older Excel

Practice file: excel-split-quantity-unit-practice.xlsx: the Grocery sheet with every step in its own column, the older-Excel version below the table, and the no-space example.

Made-up data: the items, quantities and prices are made up for practice, in the style of a quick-commerce order (think Blinkit or Amazon Now).

Which Excel has these functions? TEXTBEFORE and TEXTAFTER: Microsoft 365 and Excel for the web. NUMBERVALUE: Excel 2013 and later. Section 7 shows formulas that work in every version.

1. The raw data

A Item B Quantity C Price per kg
2 Tomato 2 kg 40
3 Potato 1.5 kg 30
4 Dal 500 gram 120
5 Apple 750 gram 160
6 Kiwi 250 gram 240

The quantities are text: they sit on the left of the cells. =SUM(B2:B6) returns 0, a favourite interview question. =ISNUMBER(B2) returns FALSE.

The plan for each row:

  1. Take the number before the space.
  2. Make it a real number.
  3. Take the unit after the space.
  4. If the unit is gram, divide by 1000 to get kg.
  5. Amount = kg × price per kg.

Classroom line

This is a favourite question of interviewers... do the SUM... what will the answer be? Zero.

2. TEXTBEFORE: the number part

=TEXTBEFORE(text, delimiter)

The delimiter is the character that separates the parts, here a space. In D2:

=TEXTBEFORE(B2," ")

→ 2. But it is the text "2": it sits on the left, and =ISNUMBER(D2) returns FALSE. TEXTBEFORE always returns text.

3. NUMBERVALUE: text to number

In E2:

=NUMBERVALUE(D2)

→ 2, now a real number on the right; =ISNUMBER(E2) returns TRUE. For "1.5 kg" it gives 1.5, for "500 gram" 500. (=VALUE(D2) does the same for these quantities.)

You can do both steps in one formula:

=NUMBERVALUE(TEXTBEFORE(B2," "))

The rule for nesting: the step that happens first goes inside, the step that happens last goes outside. TEXTBEFORE runs first, so it sits inside NUMBERVALUE.

Classroom line

Whatever happens first goes inside; whatever happens last goes on the very outside.

Split "500 gram" at the space "500 gram" TEXTBEFORE(B4," ") "500" (text, left) NUMBERVALUE(D4) 500 (number, right) TEXTAFTER(B4," ") "gram" Older Excel: =VALUE(LEFT(B4,FIND(" ",B4)-1)) and =MID(B4,FIND(" ",B4)+1,99)

Figure 1. "500 gram" split at the space: TEXTBEFORE gives the text "500", NUMBERVALUE turns it into the number 500, TEXTAFTER gives "gram".

4. TEXTAFTER: the unit

In F2:

=TEXTAFTER(B2," ")

→ kg. For row 4 it gives gram.

5. Grams to kg, and the amount

The prices are per kg, so every quantity must be in kg. In G2:

=IF(F2="kg",E2,E2/1000)

If the unit is kg, keep the number; otherwise it is grams, so divide by 1000. Then the amount in H2:

=G2*C2

Item Quantity Number Unit Qty in kg Amount
Tomato 2 kg 2 kg 2 80
Potato 1.5 kg 1.5 kg 1.5 45
Dal 500 gram 500 gram 0.5 60
Apple 750 gram 750 gram 0.75 120
Kiwi 250 gram 250 gram 0.25 60
Total 365

=SUM(H2:H6) → 365, the bill.

Classroom line

We have to convert grams into kg.

=IF(F2="kg",E2,E2/1000), then Amount = G2*C2 A B C D E 1 Quantity Unit Qty in kg Price/kg Amount 2 2 kg kg 2 40 80 3 1.5 kg kg 1.5 30 45 4 500 gram gram 0.5 120 60 5 750 gram gram 0.75 160 120 6 250 gram gram 0.25 240 60 7 Total 365 Row 4: 500 gram unit is not kg → 500/1000 = 0.5 amount = 0.5 × 120 = 60 Total = 365

Figure 2. IF(F2="kg",E2,E2/1000): 500 gram becomes 0.5 kg; amount = 0.5 × 120 = 60. The five amounts add up to 365.

Ravindra Bagale's Tip

Build a formula like this in separate helper columns first (number, unit, kg, amount) and check each column with your own eyes. Only when every column is right, combine them into one formula if you want to. A wrong answer in a nested formula is much harder to find.

6. The no-space trap

TEXTBEFORE and TEXTAFTER look for the delimiter. If a quantity is typed as "2kg" without a space, there is nothing to split at, and both return #N/A.

  1. Clean the data first, so that every quantity has one space between the number and the unit. Find & Replace (Ctrl+H) helps when the same mistake repeats, for example replacing kg with kg in cells that have no space yet.
  2. Or catch the error: =IFERROR(TEXTBEFORE(B2," "),"no space found") shows a clear message instead of #N/A, so you can fix those rows by hand.

Classroom line

It will work only if there is a space between them, remember.

7. Older Excel: LEFT, MID and FIND

Without TEXTBEFORE and TEXTAFTER (Excel 2019, 2021 and older), FIND gives the position of the space and LEFT and MID cut around it:

  1. Number: =VALUE(LEFT(B2,FIND(" ",B2)-1)). For "500 gram" the space is the 4th character, so LEFT takes 3 characters: "500".
  2. Unit: =MID(B2,FIND(" ",B2)+1,99). MID starts after the space and takes up to 99 characters: "gram".
  3. Kg and amount: the same IF and multiplication as above.

The practice file shows these formulas below the table, with the same results.

8. The whole chapter on one page

Step Microsoft 365 / web Any version Row 4 (500 gram)
Number as text =TEXTBEFORE(B4," ") =LEFT(B4,FIND(" ",B4)-1) "500"
Real number =NUMBERVALUE(D4) =VALUE(...) 500
Unit =TEXTAFTER(B4," ") =MID(B4,FIND(" ",B4)+1,99) gram
Kg =IF(F4="kg",E4,E4/1000) same 0.5
Amount =G4*C4 same 60
  1. Text quantities cannot be added: SUM returns 0.
  2. TEXTBEFORE and TEXTAFTER split at a delimiter and return text.
  3. NUMBERVALUE (or VALUE) turns the text into a number.
  4. IF converts grams to kg; then multiply by the price per kg.
  5. No delimiter means #N/A: clean the data or use IFERROR.

9. Try at home

Try at home

Task 1

  1. Add Onion, "3 kg", price 25. What is its amount, and the new total? (75; total 440.)

Task 2

  1. Add Paneer, "200 gram", price 400 per kg. What is its amount? (80.)

Task 3

  1. Type "2kg" for Tomato and see what TEXTBEFORE returns. (#N/A.)

Recap

In short

  1. "2 kg" is text; numbers must be pulled out first.
  2. TEXTBEFORE(B2," ") gives the number as text.
  3. NUMBERVALUE makes it a real number.
  4. TEXTAFTER(B2," ") gives the unit; IF converts grams to kg.
  5. Older Excel: LEFT, MID and FIND do the same job.

Quick revision cards

Quick revision

Revise this chapter in two minutes. Save the cards on your phone or download them as PNG files. All names and numbers are made up for practice.

Revision card: Split "500 gram" into number and unit. Example data table with Number part: 500; Unit part: gram; Convert to kg: 0.5.
Card 1 of 2: Split "500 gram" into number and unit · Download PNG
Revision card: Splitting text in older Excel. Example data table with Where is the space?: 4; Number part in older Excel: 500; Unit part in older Excel: gram.
Card 2 of 2: Splitting text in older Excel · Download PNG

More cards for every chapter: Ravi Sir's Cards.

Loading practice tasks...

  1. Total units sold (Easy): Add up Units sold in D2:D11. (sheet "Data")
  2. Average price (Easy): Average of E2:E11, rounded to 2 decimals. (sheet "Data")
  3. Rows in Snacks (Easy): How many rows have Category "Snacks"? (sheet "Data")
  4. Largest units sold (Easy): The largest value in D2:D11. (sheet "Data")
  5. Clean name (Easy): Trim A2. (sheet "Text")
  6. Proper case (Easy): A2 trimmed and in proper case. (sheet "Text")
  7. Code prefix (Easy): First 3 characters of B2. (sheet "Text")
  8. Code number (Easy): Last 4 characters of B3. (sheet "Text")
  9. Units sold for Snacks (Medium): Total of D2:D11 where B is "Snacks". (sheet "Data")
  10. Snacks in Mumbai (Medium): Total of D2:D11 where B is "Snacks" and C is "Mumbai". (sheet "Data")
  11. City part (Medium): Characters 5 to 7 of B4. (sheet "Text")
  12. Name length (Medium): Length of A3. (sheet "Text")