Excel Formulas for Removing Non-Alphanumeric Characters from Text Strings

📅 Oct 09, 2026 📝 Sarah Miller

Data analysts often struggle with dirty text strings containing unwanted symbols that disrupt database imports and lookups. When consolidating financial reports from standard funding sources, inconsistent system formatting frequently corrupts critical identifiers. Mastering a tailored Excel formula to extract only alphanumeric characters grants you complete control over data integrity and reporting speed. As a key stipulation, note that native Excel lacks a direct regex function, requiring nested array formulas to achieve this. For instance, cleaning strings like "ID#_99-A" into "ID99A" ensures seamless system alignment. The following guide provides the exact formula structure to automate this cleanup.

Excel Formulas for Removing Non-Alphanumeric Characters from Text Strings

Data cleaning is one of the most common, yet tedious, tasks in data analysis. When importing data into Microsoft Excel from external sources-such as web scraping, SQL databases, PDF conversions, or legacy ERP systems-you often inherit a messy trail of non-alphanumeric characters. These can include punctuation, trailing spaces, mathematical symbols, control characters, or non-printable ASCII codes.

These unwanted characters can wreck your VLOOKUPs, disrupt your XLOOKUPs, break pivot tables, and corrupt data analysis. While Excel provides native functions like TRIM (which removes excess spaces) and CLEAN (which removes non-printable characters), they fall short when you need to completely strip out symbols like hyphens, hashtags, asterisks, or currency signs while keeping only letters and numbers.

In this comprehensive guide, we will explore several powerful techniques to trim and remove non-alphanumeric characters from text strings in Excel, ranging from the brand-new Microsoft 365 native Regular Expression functions to Power Query, VBA, and classic formulas.


Method 1: The Modern Way – Using Native Excel Regex (Microsoft 365)

For decades, Excel users clamored for native Regular Expression (Regex) support. Microsoft finally answered that call with the introduction of new Regex functions. If you are using Microsoft 365 (Insider or Current Channel), you can use the incredibly powerful REGEXP.REPLACE function to clean your text strings in a single step.

The Formula

=REGEXP.REPLACE(A2, "[^a-zA-Z0-9]", "")

How It Works

  • A2: The target cell containing the messy text string.
  • [^a-zA-Z0-9]: This is the regular expression pattern.
    • The square brackets [] define a character set.
    • The caret ^ inside the brackets acts as a negation operator (meaning "not these characters").
    • a-z, A-Z, and 0-9 represent all lowercase letters, uppercase letters, and numerical digits.
    • Combined, [^a-zA-Z0-9] targets any character that is not an alphanumeric character.
  • "": The replacement string. We replace every matched non-alphanumeric character with an empty string, effectively deleting them.

This is by far the cleanest, fastest, and most scalable formula-based approach available in modern Excel.


Method 2: Power Query (The Best for Large Datasets)

If you are working with thousands of rows of data, running complex array formulas can slow Excel to a crawl. In such cases, Power Query is the superior tool. It is built directly into Excel and features a robust function language called "M Code" that can clean text with ease.

Step-by-Step Guide

  1. Select your data range and go to the Data tab, then click From Table/Range.
  2. Once the Power Query Editor opens, go to the Add Column tab and click on Custom Column.
  3. Name your new column (e.g., Clean_Text).
  4. In the Custom Column formula box, enter the following M Code formula:
Text.Select([YourColumnName], {"a".."z", "A".."Z", "0".."9"})

How It Works

The Text.Select function extracts only the characters specified in the second argument. By providing lists of lowercase letters ({"a".."z"}), uppercase letters ({"A".."Z"}), and numbers ({"0".."9"}), Power Query discards everything else-including spaces, punctuation, and special symbols-leaving you with a perfectly clean alphanumeric string.

To keep spaces, you can easily append a space to the list: Text.Select([YourColumnName], {"a".."z", "A".."Z", "0".."9", " "}).


Method 3: The Classic VBA User-Defined Function (UDF)

If your organization does not use Microsoft 365 yet, or if you need a solution that runs locally on legacy versions of Excel (Excel 2013, 2016, or 2019) without Power Query overhead, VBA (Visual Basic for Applications) is your best friend. We can create a custom formula called =RemoveSpecialChars(text).

The VBA Code

Press ALT + F11 to open the VBA Editor, click Insert > Module, and paste the following code:

Function RemoveSpecialChars(Txt As String) As String
    Dim RegEx As Object
    Set RegEx = CreateObject("VBScript.RegExp")
    
    With RegEx
        .Global = True
        .IgnoreCase = True
        .Pattern = "[^a-zA-Z0-9]" ' Matches any non-alphanumeric character
    End With
    
    RemoveSpecialChars = RegEx.Replace(Txt, "")
End Function

How to Use It

Close the VBA window, return to your Excel worksheet, and use your newly created custom function just like any native Excel formula:

=RemoveSpecialChars(A2)

This macro utilizes the Windows scripting engine's regular expression library to match and replace symbols instantly, ensuring compatibility across older Excel versions.


Method 4: The Legacy Formula Approach (LAMBDA & REDUCE)

If you are on Excel 365 but don't have access to the newer REGEXP.REPLACE function yet, you can build a formulaic loop using the LAMBDA and REDUCE helper functions. This method does not require VBA or Power Query.

The Formula

=REDUCE(A2, {" ","-","_","/",".","@",",","*","?","!","(",")"}, LAMBDA(text,char, SUBSTITUTE(text, char, "")))

How It Works

  • REDUCE: Iterates through an array of characters (defined in curly braces) and applies a custom calculation step-by-step to the initial value (A2).
  • LAMBDA(text, char, ...): Instructs Excel to take the current state of the text (text), take the current character from the array (char), and perform a function.
  • SUBSTITUTE(text, char, ""): Substitutes the targeted special character with nothing, incrementally cleaning the text with every iteration.

Note: This method is highly customizable. You can explicitly define which characters to remove. However, if your data contains hundreds of different symbols, you would have to define all of them inside the array.


Method 5: Trimming Only Leading and Trailing Non-Alphanumeric Characters

Sometimes, you do not want to strip special characters from the middle of a text string (e.g., keeping spaces or hyphens inside "Abc-123 Code"), but you do want to trim off any messy symbols, spaces, or slashes clinging to the beginning or the end of the string.

This is a more nuanced problem. We can combine modern dynamic arrays to create a formula that trims leading and trailing non-alphanumeric noise:

=LET(
    chars, MID(A2, SEQUENCE(LEN(A2)), 1),
    is_alphanum, MAP(chars, LAMBDA(c, AND(OR(AND(CODE(UPPER(c))>=65, CODE(UPPER(c))<=90), AND(CODE(c)>=48, CODE(c)<=57))))),
    first_idx, MATCH(TRUE, is_alphanum, 0),
    last_idx, XMATCH(TRUE, is_alphanum, 0, -1),
    MID(A2, first_idx, last_idx - first_idx + 1)
)

Breakdown of this Advanced Formula:

  • chars: Splits the string in cell A2 into an array of individual characters.
  • is_alphanum: Maps through each character, checking its ASCII value. It returns TRUE if the character is uppercase/lowercase A-Z (ASCII 65-90) or 0-9 (ASCII 48-57).
  • first_idx: Uses MATCH to find the index of the very first alphanumeric character.
  • last_idx: Uses XMATCH with search direction -1 (reverse search) to locate the index of the last alphanumeric character.
  • MID: Extracts the substring starting from the first clean character to the last clean character, leaving all middle punctuation intact while shedding outer noise.

Choosing the Right Tool for the Job

Depending on your Excel version and data volume, the ideal solution varies:

Method Best For Excel Compatibility Pros / Cons
REGEXP.REPLACE Quick, modern formula cleaning Excel 365 (Beta / Current) ⚡ Fast, simple syntax, dynamic. Needs modern Excel.
Power Query Large scale ETL / Data pipelines Excel 2010 and later ? Extremely robust. Handles millions of rows without lagging.
VBA UDF Universal backward compatibility All Desktop Versions ⚙️ Runs anywhere. Requires macro-enabled workbook (.xlsm).
REDUCE / LAMBDA Targeting specific custom symbols Excel 365 / Web ? No VBA required. Manual character array setup.

Conclusion

Sanitizing your datasets is key to error-free lookup operations and reporting. If you have moved to Microsoft 365, embracing the new REGEXP.REPLACE function or utilizing Power Query's Text.Select will streamline your data preparation process. For older workbooks, dropping in a short VBA regex block keeps your sheets light and functional. Implement these workflows today to spend less time scrubbing messy data and more time extracting valuable insights!

Disclaimer:
The documents and templates provided on this page are for informational and illustrative purposes only. They do not constitute professional, legal, or financial advice, and should not be relied upon as such. Because individual circumstances and regulatory requirements vary, these materials may not be suitable for your specific needs. We recommend consulting with a qualified professional before adapting or using any of these examples for official or commercial purposes.