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

3. Formulas and Functions

Excel

3.9 TEXTBEFORE, TEXTAFTER and TEXTSPLIT

Let the separators identify the fields

A fictional Blinkit-inspired pickup worksheet stores a location as PUN-Kothrud-01. A second location reads PUN-Old-Town-01. The extra hyphen belongs in the area name. Before you split the values, decide what the first separator, last separator and middle characters mean.

All example codes, locations and branch assignments are invented for learning. The examples describe no actual import format or internal policy at Blinkit or Swiggy. A delimiter is the character or text between fields. When a delimiter is part of the documented layout, you can find fields without counting fixed numbers of characters from the start.

PUN-Old-Town-01 has PUN before the first hyphen, 01 after the last, and Old-Town between the two.
Decide which separator belongs to the field boundary. An internal hyphen can be part of the field itself.

Check whether your Excel client supports these functions

Microsoft lists these functions in supported Excel for Microsoft 365 and Excel 2024 clients, including corresponding Mac versions and Excel for the web in the current documentation. Exact client and update channels may differ. They are outside the documented Excel 2021 feature set. Before using them in a shared file, type a small expression and check that everyone who will calculate it has a supported version.

For a simple availability test, try =TEXTBEFORE("A-B","-") in a blank cell. It should return A in a client that supports the function, using the argument separator accepted by your installation. Try =TEXTSPLIT("A-B","-") in an area whose neighboring cell is empty. Expect A and B in adjacent cells. An unrecognized function or a version-related name marker may mean the workbook needs a compatibility plan.

The earlier text extraction lesson shows how to combine LEFT, RIGHT, MID and FIND for common cases on older versions. A supported Text to Columns workflow is another way to split a scratch copy of a fixed export. That command writes results to cells; these formulas calculate from the current source when the workbook recalculates.

Choose the delimiter instance before extracting

The instance is which occurrence of the delimiter you select. With the practice code PUN-Old-Town-01, the first hyphen follows PUN, the second follows Old and the last follows Town. With a positive instance count, Excel counts matches from the start. With a negative count, it counts matches from the end.

Assume A2 contains PUN-Old-Town-01. Each expression returns one text result.
ExpressionExpected textSelected boundary
=TEXTBEFORE(A2,"-")PUNBefore the first match
=TEXTAFTER(A2,"-")Old-Town-01After the first match
=TEXTBEFORE(A2,"-",2)PUN-OldBefore the second match from the start
=TEXTBEFORE(A2,"-",-1)PUN-Old-TownBefore the last match
=TEXTAFTER(A2,"-",-1)01After the last match

Read the optional arguments separately

  • Instance number. The ordinary default is one match from the start. Use -1 for the last occurrence. A zero instance is invalid. A match request beyond the available delimiters can return an error.
  • Match mode. Use 0 for case-sensitive delimiters and 1 for case-insensitive matches. The default is case-sensitive. For a punctuation-only hyphen the case argument makes no difference.
  • Match end. When requested, Excel can treat the end of the text as a delimiter. This is useful for an intentional rule for a one-token label. The default does not add a virtual end boundary.
  • If not found. Supply an explicit text result for a missing requested delimiter when that is the expected review path. The default for no match is #N/A. Other malformed arguments, such as an instance of zero, still need a formula fix.

The delimiter can be a multi-character string. For a source with City::Pune, use "::" as a whole separator. TEXTAFTER returns Pune. If you used a single colon in that example, a colon would remain at the start of the returned text. Write the delimiter as it appears in the actual data.

Lab 1 · Read a city, a variable-length area and a branch

Create a scratch sheet called LocationParts. Put Raw code, City prefix, After first hyphen, Area and Branch text in columns A through E. The documented classroom layout places the city before the first hyphen and the branch after the last. Everything between those two separators forms the area, including any hyphen that belongs to the area name.

Keep the original code in A. Fill the other fields only after checking the source boundaries.
Raw codeCity prefixAreaBranch text
PUN-Kothrud-01PUNKothrud01
PUN-Old-Town-01PUNOld-Town01
NSK-College Road-01NSKCollege Road01
SOLAPUR-West-03SOLAPURWest03

Prove each extraction before nesting it

  1. In B2 enter =TEXTBEFORE(A2,"-") for the city prefix. It returns PUN. Check the Solapur row after filling it down. The longer prefix still returns completely because the boundary is the first hyphen.
  2. In C2 use =TEXTAFTER(A2,"-"). It returns Kothrud-01 for the first row. In row 3, its result is Old-Town-01. This temporary field shows what remains after removing the first prefix and its separator.
  3. In D2 enter =TEXTBEFORE(C2,"-",-1). The input is the shortened text from the previous stage. The final hyphen identifies the branch boundary. D3 becomes Old-Town and preserves the hyphen belonging to the area.
  4. In E2 use =TEXTAFTER(A2,"-",-1). The result is the branch text 01. Leave its leading zero intact because the branch is an identifier. Try =ISTEXT(E2) in a blank cell to verify its type.
  5. Fill B2 through E2 down to row 5. Verify all four inputs and outputs. In a spare cell compare the area result with =TEXTBEFORE(TEXTAFTER(A2,"-"),"-",-1). The nested formula uses the same two operations in order. The helper columns make it easier to understand which boundary changed.

Test which exceptions require a review label

On a separate row, try PUN with no delimiter. A plain TEXTAFTER asking for a hyphen returns a no-match error. A formula such as =TEXTAFTER(A6,"-",1,0,0,"Review missing delimiter") gives the exercise's meaningful fallback if A6 contains that value. An empty result would give less information because it could also mean that a valid delimiter was followed by an empty field.

Try PUN-01 as a one-delimiter version. The prefix and branch expressions both return plausible text. The middle extraction needs another hyphen after the first and gives an error. If the area must be nonempty, also reject PUN--01. Both boundaries exist there, but extracting between them gives the empty string.

Read the shape that TEXTSPLIT will produce

TEXTSPLIT handles a different job from taking one piece before or after a selected match. You give it the text and the separators for output columns or rows. It builds an array, a set of values, and puts those values into cells next to the formula. This is called spilling. The formula lives in the top-left cell of that output range. Its neighboring results are managed by that one formula.

Know which positions refer to the output

The middle arguments control the grid shape rather than selecting a delimiter instance.
ArgumentRole
TextThe source string or reference you want to split.
Column delimiterA boundary between pieces spilling across a row.
Row delimiterA boundary between lines spilling down the worksheet. You can omit the column delimiter if you only want to split by rows.
Ignore emptyFALSE keeps empty pieces, which is the default. TRUE drops empty positions caused by consecutive separators.
Match modeZero matches case and is the default. One makes delimiter matching case-insensitive.
Pad withThe result used to fill missing cells in a rectangular array. The default is the #N/A result.

For a simple one-row split such as =TEXTSPLIT(A2,"-"), give the source and the column delimiter. No instance count is part of this function's syntax. The third argument is a row delimiter, even though the third argument of TEXTBEFORE or TEXTAFTER denotes an instance. Check the formula hint carefully when moving between these functions.

Predict the table dimensions first

Using a column delimiter on the three-part code PUN-Kothrud-01 implies one row of three results. Using a row delimiter alone yields three rows in one column. You can also use both directions at once to unpack a carefully formatted practice string. Start with two known records and check how far the result should spread down and across.

Lab 2 · Clear a blocked spill and preserve fields

Create a new ordinary worksheet named SplitGrid. Put PUN-Kothrud-01 in A2. This time the source has exactly two delimiters and each one marks a field boundary. The split should give three pieces. Reserve B2 through D2 as the destination, with B2 as the place where the formula lives.

Reproduce a blockage in a practice copy

  1. Temporarily type Note in D2. This represents a preexisting entry in the spill's third destination cell. Then enter =TEXTSPLIT(A2,"-") in B2. It should show #SPILL! because the result needs D2.
  2. Select B2. In a client that highlights the intended output range, check that the last cell lies on D2. The raw cell A2 remains valid. The error describes the output area, not a problem finding hyphens.
  3. Remove the practice note from D2. In a real worksheet you would move an existing note somewhere appropriate before clearing the destination. B2, C2 and D2 should now contain PUN, Kothrud and 01 as the three visible pieces.
  4. Select C2 or D2 and inspect the formula bar. Those are returned values managed by the formula in B2. To change the rule, edit the top-left formula. To alter the source, edit A2. Do not type over a result cell as if it held a separate formula.
B2 contains the TEXTSPLIT formula, with results intended for B2 through D2. A note in D2 blocks it with a spill error; clearing the note lets all three results appear.
A correct separator rule still needs a clear destination range.

Step through the destination cells

  1. Predict the shape first

    Two separators create three parts. A formula anchored at B2 intends to use B2, C2 and D2.

  2. Observe the note in the last cell

    D2 already holds a practice note. Excel refuses to replace it. The spill formula reports a blocked output.

  3. Free the intended output

    Clear the temporary note so the three cells are available. The existing formula can now place each part in its intended position.

  4. Read the pieces and the source

    B2 holds PUN, C2 holds Kothrud and D2 holds the branch text 01. A2 still holds the original full value.

Compare keeping and skipping an empty middle field

In A4 enter NSK--01. In B4 put =TEXTSPLIT(A4,"-"). It keeps the empty middle position by default. B4 is NSK, C4 displays empty, and D4 is 01. To test the other policy in a separate clear area, put =TEXTSPLIT(A4,"-",,TRUE) in F4. It shows NSK and 01 as two adjacent pieces. Branch moves to the second result because the middle field was skipped.

If the goal is three named fields, the first layout makes clear which field is missing. If the goal is a compact list of available parts, the second layout can be useful. Test the representation after the split. An empty-looking result within a spill still belongs to that output range, so keep other worksheet content out of the region.

Build a two-direction output from a small fixture

A fictional route-planning exercise receives two store labels bundled in one string. The source uses commas between fields and semicolons between records. It is safe to split this practice input because the fixture guarantees none of the field values contains those two separator characters. A production CSV file can have quoted separators and needs a parser that understands its format.

Put Kothrud,Pune,411038;Baner,Pune in A8 on SplitGrid. Leave B8:D9 empty. The second record deliberately omits the postal code at the end. The postal codes here are labels in the worksheet, not numbers you need to add.

  1. In B8 enter =TEXTSPLIT(A8,",",";"). The comma divides values within each row. The semicolon divides records down the sheet. Predict two output rows and three columns based on the longer record.
  2. Check row 8. It has Kothrud, Pune and 411038 in B, C and D. Check row 9. It has Baner, Pune and an #N/A padding result in D9. The default padding fills the missing part of the shorter row so the array is rectangular.
  3. Move to a separate clear area and try =TEXTSPLIT(A8,",",";",FALSE,0,"Missing"). Now the shorter row's padding shows Missing. Use a label like this when it helps a reader understand that a field was not supplied. It is different from a real input containing the word Missing.
  4. To test how padding differs from an empty field, add a training record Camp,,411001 at the end, preceded by a semicolon. The adjacent commas express an explicit empty city position even though a postal code follows. A field can be present in the layout but contain no text. A shorter record can lack its final position entirely. Check the expected shape before treating these states as equivalent.

Let a column or row delimiter be omitted where appropriate

For a vertical list from the clean text Pune,Nashik,Nagpur, use a column delimiter placeholder and a row delimiter: =TEXTSPLIT(A10,,",") when A10 has that text. The results occupy three rows in one column. In those examples commas outside quotation marks are argument separators and the comma inside quotes is the character being found. Use the function hint if your Excel setup expects different argument separators.

If a particular source allows either a pipe or a slash as the column delimiter and never uses either inside a field, you can pass an array of the allowed delimiters. For example, =TEXTSPLIT("Pune|Nashik/Nagpur",{"|","/"}) returns three neighboring pieces. The braces describe a group of possible delimiter strings in this configuration. Array-constant punctuation can differ with locale. Verify the result against both separators before applying it to a file.

Make a missing delimiter an explicit input state

For a pickup code, the absence of a required hyphen makes the record incomplete. For a free-form name with one word, a missing space could be expected. The same no-delimiter outcome can mean different things depending on the field. Choose a specific handling rule rather than always using an empty fallback.

Choose end-of-text matching only when it fits the rule

In a scratch area suppose you want the first word of a one- or two-word area label. Try =TEXTBEFORE("Kothrud"," ",1,0,0,"Review missing space"). The single-word input contains no match, so it uses the fallback. Now test =TEXTBEFORE("Kothrud"," ",1,0,1,"Review missing space"). With end matching enabled, the end of Kothrud can stand in for the boundary, and the expression returns Kothrud. With College Road, it finds the earlier actual space and returns College.

That rule is useful when a standalone field is valid. In the location-code lab, you want the absence of a required delimiter to raise attention. Choosing end matching there could accidentally accept a whole code with no actual separator. Write a known one-word case and a missing-required-delimiter case. They should reach different branches if the data policies differ.

Use the case argument for text delimiters

In a fixture where labels start with a marker like CITY:, compare =TEXTAFTER("CITY:Pune","city:",1,0,0,"No marker") with the same call using 1 for match mode. The first gives No marker, because the literal letter case differs. The second finds the case-insensitive match and returns Pune. If case carries meaning in the source, preserve the strict rule and put mismatched samples in a review column.

A hyphen or pipe has no uppercase form, so changing match mode will not fix a delimiter that was replaced by a different Unicode glyph. Inspect the source text or compare lengths and code points to confirm which actual character is being used. Use only the delimiter alternatives the source contract allows.

Practice extension · separate two fields around an at sign

A fictional support worksheet has a synthetic string zoya@example.com. You want the text before the at sign and the text after it in separate cells. In this bounded exercise, the expected input shape includes exactly one at sign and nonempty text on either side. The result extracts fields for a practice report. It does not verify that an address is deliverable.

  1. Enter the sample into A2 on a new tab. Try =TEXTBEFORE(A2,"@") in B2. It gives zoya. Confirm that the delimiting at sign is excluded.
  2. In C2 enter =TEXTAFTER(A2,"@"). It gives example.com. The dot already belongs to the domain string, so this formula treats it as part of the returned field.
  3. Try team.example.com in a new row. No at sign exists, so the plain extraction has no match. A fallback such as Review separator can help if your output needs a readable marker. Keep the original value for diagnosis.
  4. Try zoya@@example.com. The first TEXTAFTER returns a string beginning with another at sign. TEXTAFTER using the last instance instead would return example.com. A plausible output is not proof that the raw field meets the expected shape. Count the original occurrences first for this restricted exercise.

For a single-character delimiter, one way to count occurrences is the length of the original string minus the length after removing all occurrences of that character with SUBSTITUTE. The original zoya@example.com has one at sign. The double-at test has two and should go to Review under this lesson's rule. Add a check for nonempty text on both sides. A valid shape for this exercise is a much narrower claim than general email-address validation.

As a comparison, you can use TEXTSPLIT on the at sign and expect two pieces only for the valid example. If your extraction needs one output used in a specific report cell, TEXTBEFORE or TEXTAFTER gives that one result without needing a large destination. Use a spill when the neighboring cells are explicitly reserved for the multiple parts.

Check the actual output width without guessing

The result of a split changes shape when the source contains more or fewer delimiters. A static reference to B2 through D2 covers three cells even if the formula now returns four. In a workbook whose client supports spill references, use the spilled range operator on the formula's anchor. A reference such as B2# means the whole currently returned range that starts at B2.

Test the separate width check on SplitGrid

  1. Use the row 2 split after removing the blocking note. In a clear space away from the output, enter =COLUMNS(B2#). It returns three for the original PUN-Kothrud-01 result.
  2. Make sure E2 is also free. In the practice copy temporarily change A2 to PUN-Old-Town-01. The selected all-hyphen rule now returns four pieces across B2 through E2. The width check returns four, which violates the expected three-field contract for that version of the exercise.
  3. Restore the shorter code and verify the width returns to three. The same formula follows the dynamic result. Checking output width identifies a shape issue. It does not prove the fields themselves are valid.
  4. For the short original code, use =TEXTJOIN("-",FALSE,B2#) in a separate clear cell. It reassembles the three nonempty pieces using one hyphen between them. Compare with A2 to verify the intended characters on this constrained fixture.

Keeping these checks in the same workbook avoids cross-workbook spill-reference limitations. Microsoft documents that a spill reference to a closed workbook is unsupported. If you need to share extracted results as a snapshot, build a separate values-only output once the source and extraction have been verified.

Read the symptom before trying a different formula

Begin at one known source row with a predictable result. Then confirm the client, the matched delimiter, the intended number of pieces and the destination area. These checks cover distinct failure modes. Changing a separator to work around a grid obstacle risks breaking a rule that was already correct.

  • The function name is unrecognized. Check the exact spelling and your version. A workbook might show an unrecognized function when opened in an older release. Use an established compatible formula or one-time split workflow when the audience cannot calculate the newer function.
  • A one-piece result shows a no-match error. Verify the text and delimiter instance. A required second separator may be absent even though the first is correct. Inspect the raw source and the explicit review policy.
  • A multiple-piece result shows a spill error. Look for an existing value, a merged cell, too little space or a formula entered inside an Excel Table. A visible blank can hold a formula or an invisible character. Move the split to an adequate empty grid area if appropriate.
  • Some returned cells show an error marker while other pieces appear. Check whether the row is shorter than another row in a rectangular split. TEXTSPLIT's default padding can display #N/A in the missing output positions. Inspect how many fields were actually supplied before changing the pad value.
  • The result is shifted a column. Check whether ignore-empty was enabled. An empty middle section can disappear and move later pieces left. A preserved layout distinguishes an empty city field from a branch field.
  • The source is a Table. You can use source values from the Table while placing a spilling formula in the ordinary grid outside it. A Table calculated column can hold a one-result formula when compatible with the client. Put the multiple-cell result outside the Table so it can expand.

Use a literal fixture with just two fields before diagnosing a long parse. Enter a known source and define the expected text or grid. If the minimal example works, change one property at a time: case, delimiter, length or destination. This makes it clear which difference explains the actual workbook problem.

Choose between a live formula and a one-time conversion

Suppose a fictional marketplace seller downloads the same structured text report every morning. A live formula can recalculate the outputs from each new input. When the workbook is designed with a reserved destination, the operator can check the split as soon as the source is replaced. The formula also leaves a clear record of the chosen delimiter and empty-field policy.

For a one-time export, the Text to Columns workflow can write the split pieces into a destination range in supporting clients. Work in a copy. Pick a destination that has room for the output and preserve the untouched source. For an identifier such as branch 01, check whether the splitting workflow offers a Text data type for that result. A numeric interpretation can drop its leading zero. Confirm exact string values after conversion.

Save a small comparison file

  1. Use the original PUN-Kothrud-01 as a shared input. In the live design, put its formula result outside an Excel Table with space for neighboring pieces.
  2. Record what the static version produces from the same raw value. Check the branch text 01 and the two word boundaries. If a tool decides to import that branch as a number, it may display only 1.
  3. In a scratch copy of both results, update the source to NSK-College Road-01. The calculated version should show NSK and College Road under the same simple two-hyphen rule. The static result remains as generated until you run the conversion again. That difference tells you which workflow fits a repeated report.
  4. Before using the method on a new file, try the internal-hyphen Old-Town example. A split at all delimiter occurrences yields four pieces. If the agreed layout needs three fields with Old-Town intact, use the first-and-last extraction from Lab 1 or another documented parsing rule.

When handing off a workbook, put a few approved input examples near the description of its layout. Give a simple valid row, a variable-length row and a result expected to go to review. Name the Excel features required to run the formula. If the team needs to retain the original text for later inspection, leave it in the source field and export derived values separately.

Keep retained fields and skipped fields distinguishable

A compact list of available tokens may be fine for a display, while a structured output used by another task needs field positions. A skipped middle value makes the original column meaning ambiguous. In the pickup labels, the city and branch should remain in known columns even when one is absent. Verify the final shape and type of every returned token before using the result as part of another calculation.

Decide whether a space is part of the delimiter

In a separate scratch area, enter Kothrud, Pune, 411038 as one value. Splitting on just the comma gives three output parts, and the second and third parts can still contain their leading spaces. Compare the second returned value with a separate cell containing Pune. The visible text can look the same at first. LEN gives five for the space plus Pune and four for the exact city label.

If the input always uses a comma followed by exactly one ordinary space, a delimiter of ", " can remove both characters as the field boundary. If some rows omit the space, that exact two-character delimiter will miss those boundaries. Test the same rule on Kothrud,Pune,411038. When the source convention is inconsistent, splitting on comma and normalizing known padding afterward gives you an explicit decision you can audit.

Record the expected column count alongside the separately expected values. A rule using the exact two-character delimiter on the no-space example misses both boundaries. A rule using a plain comma on the spaced example gets the right number of columns and leaves extra characters in two results. Check both the shape and the field contents to distinguish these failures.

  1. Test the field values by comparing them with independently entered expectations. Include a city with an intentional internal space, such as Sambhaji Nagar, to confirm that valid words stay together.
  2. Keep the original postal labels as text when leading zeros or exact codes matter. A visually numeric-looking text result can still be the desired representation.
  3. Confirm that the comma is a field separator in this particular practice input. A comma within a real street address could change the split. Choose the mechanism using the export's documented rules.

Quiz · predict the field and the output shape

What does TEXTAFTER with instance -1 find in PUN-Old-Town-01?

It returns 01 after the last hyphen. Counting from the end selects the branch boundary while preserving the code's leading zero in the text result.

Should you split at every hyphen to keep Old-Town as one area?

Under that source contract, use the first and last delimiters to enclose the area. The all-hyphen split produces Old and Town as separate pieces. It is solving a different boundary rule.

What should you inspect when the split in B2 shows a spill error?

Check the intended cells, including D2 for the third piece. A note, merged cell or other existing output may block the formula. Fix the destination rather than changing a valid delimiter.

Why does the last result in the shorter store row show #N/A?

The rectangular result needs a cell for every column position. The shorter record did not supply the third field, so TEXTSPLIT uses its default padding. You can choose a deliberate pad value if the report needs one.

Does TEXTSPLIT use the third argument as a match occurrence?

Its third argument specifies the row delimiter. Instance numbers belong to TEXTBEFORE and TEXTAFTER. Read the line of arguments for the particular function before pasting a known number into it.

What happens when you ignore empty fields in NSK--01?

There are only two displayed pieces, NSK and 01. The branch moves into the second position. Keep empty pieces when a report requires the middle field and its absence to remain visible.

Interview practice · explain the source rule

When would you use TEXTBEFORE or TEXTAFTER instead of TEXTSPLIT?

I would select a single field before or after a known delimiter instance when I only need one value. I would use TEXTSPLIT for the full array when the delimiters describe all boundaries and the report has room for the result.

How would you handle an area name that includes a hyphen?

I would read the source contract. When the first hyphen ends the city prefix and the last begins the branch, I can take the contents between them. A test row such as PUN-Old-Town-01 proves that the internal hyphen stays in the area.

What is a spilled array in a worksheet?

One formula returns multiple values and Excel fills them into a neighboring range. I edit the top-left anchor to change the rule and keep the complete intended output space free. I would check the width when the input can vary.

How would you distinguish a missing field from a bad source record?

I would specify the required pieces first, then test for required delimiters and expected field count. An empty field can occupy a valid position, while missing boundaries may violate the record layout. I would keep the raw string and provide a scoped review outcome.

How would you share the result with an older Excel user?

I would verify which formulas their version recognizes and choose compatible positional formulas or a separately verified values-only output when needed. The source sheet and text contract let the recipient review the output without relying on unavailable functions.

Six cards to recall a split rule

  • Delimiter The source's actual boundary string. Check whether it can also appear as content.
  • TEXTBEFORE Returns the part before one selected instance. Choose the required start or end direction.
  • TEXTAFTER Returns the part after one selected instance. A negative instance counts from the end.
  • TEXTSPLIT Returns an array using column delimiters, row delimiters or both. Give it the required grid space.
  • Empty and padded cells An empty field can keep its position. Padding fills cells missing from shorter rows in a rectangular result.
  • Source audit Check version, delimiters, field types and the shape on a verified example before using the output elsewhere.

Continue to the date functions lesson. If your client needs a compatible text formula, revisit fixed widths and FIND boundaries.

Microsoft references

Continue practising: explore the existing Excel Task Hub for additional exercises.