Share via

Excel substitute function not working as expected

Jerry Beccia 20 Reputation points
2026-03-21T18:21:56.99+00:00

Data in cell A1 is (16) UMBC vs. (16) Howard

Function in cell A2 is: Substitute(A1," ","|",4)

Function returned (16) UMBC vs. (16) Howard - exactly what was in cell A1

It did not replace the 4th space with the | as expected: (16) UMBC vs. (16)|Howard.

Anyone know what I'm doing wrong?

Microsoft 365 and Office | Excel | For home | Windows
0 comments No comments

Answer accepted by question author
  1. EmilyS726 222.1K Reputation points Independent Advisor
    2026-03-21T18:58:44.99+00:00

    Try this:
    =SUBSTITUTE(TRIM(A1)," ","|",4)

    Let me know if this is the result you are expecting?

    User's image


1 additional answer

Sort by: Most helpful
  1. Q&A Assist
    2026-03-21T18:22:04.65+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    SUBSTITUTE counts every character that Excel recognizes as a space, including non‑breaking spaces (often pasted from web pages), but the instance_num argument refers to occurrences of the exact old_text string.

    In this case, the “spaces” in the text are not all the same character. Some are likely regular spaces, and at least one is a non‑breaking space (CHAR(160)), so " " is not matching the fourth separator as expected.

    To fix it, first normalize all non‑breaking spaces to regular spaces, then apply SUBSTITUTE. For example:

    =SUBSTITUTE(SUBSTITUTE(A1,CHAR(160)," ")," ","|",4)
    

    Explanation:

    1. SUBSTITUTE(A1,CHAR(160)," ") converts all non‑breaking spaces to normal spaces.
    2. The outer SUBSTITUTE(...," ","|",4) then correctly replaces the 4th regular space with |.

    If needed, adjust the instance_num (the 4th argument) to target a different occurrence.

    For reference, SUBSTITUTE syntax is:

    SUBSTITUTE(text, old_text, new_text, [instance_num])
    

    Where instance_num is optional; if omitted, all occurrences of old_text are replaced.


    References:

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.