How to Use VBA to Remove Numbers from a String in Excel – 4 Methods

This is an overview.

VBA code to Remove Numbers from String


Below is the sample dataset.

Dataset to Remove Numbers from String


Method 1 – Delete Leading Numbers from String with Excel VBA

Steps:

  • Press Alt + F11 or go to Developer > Visual Basic to open Visual Basic Editor.

Choosing Visual Basic from Developer Tab

  • In the Microsoft Visual Basic for Applications window, click Insert > Module.

Selecting Module from Insert Tab to Insert VBA

  • Enter the following code in the code window.
Public Function Delete_Lead_Num(val As String) As String
Dim l As Long
For l = 1 To Len(val)
Select Case Mid$(val, l, 1)
Case "0" To "9"
Case Else: Exit For
End Select
Next
Delete_Lead_Num = Mid$(val, l)
End Function

VBA code to Remove Numbers from String

Code Breakdown

  • A User Defined Function (UDF): Delete_Lead_Num is created.
  • l=1 is declared to Len (value) to calculate the number of characters in the text string with the LEN function.
  • The MID function gets a specific number of characters between 0 to 9 and is declared as Case.
  • The Delete_Lead_Num function deletes the leading numbers returned by the Mid$(val, l, 1) argument.
  • Click Save to save the macro file as a User Defined Function (UDF).

Saving VBA Code

  • Go back to the worksheet and enter the user-defined function created in the VBA code and inside the brackets of the function, use the cell reference number: B5.
  • The formula is:
=Delete_Lead_Num(B5)

Applying Formula to Delete Leading Numbers from String in Excel

  • Press Enter.

The string is displayed without leading numbers.

  • Drag down the Fill Handle to see the result in the rest of the cells.

Final Output on Using VBA to Delete Leading Numbers from String in Excel


Method 2 – Using VBA to Remove Trailing Numbers from a String

Steps:

  • Open the  Visual Basic Editor in the Developer tab and Insert a Module.
  • Enter the following code in the code window.
Function Remove_Trail_Number(stdTxt As String)
Dim strg As String, x As Integer, y As Integer
stdTxt = Trim(stdTxt)
y = 1
For x = Len(stdTxt) To 1 Step -1
If IsNumeric(Mid(stdTxt, x, 1)) Then
strg = Left(stdTxt, Len(stdTxt) - y)
y = y + 1
Else: GoTo done
End If
Next x
done:
Remove_Trail_Number = Trim(strg)
End Function

Inserting VBA Code to Remove Trailing Numbers from String

Code Breakdown

  • A User Defined Function (UDF): Remove_Trail_Number is created.
  • x=Len (stdTxt) is declared to calculate the number of characters  with the 1 Step -1 argument in the LEN function.
  • The MID function is used to get a specific number of characters and is nested with the ISNUMERIC function to return the value as a number.
  • y is declared as Integer and is subtracted from the text with the argument Left(stdTxt, Len(stdTxt).
  • The Trim (strg) argument  splits spaces in the  Remove_Trail_Number function.
  • Press Ctrl + S to save the code.
  • Enter the following formula and press Enter.
=Remove_Trail_Number(B5)

You will see the string without trailing numbers.

  • Drag down the Fill Handle to see the result in the rest of the cells.

Output of Applying VBA to Remove Trailing Numbers


Method 3 – Extract All Numbers from a String Using VBA in Excel

Steps:

  • Open the  Visual Basic Editor in the Developer tab and Insert a Module.
  • Enter the following code in the code window.
Function Remove_Number(Text As String) As String
With CreateObject("VBScript.RegExp")
.Global = True
.Pattern = "[0-9]"
Remove_Number = .Replace(Text, "")
End With
End Function

Using VBA to Extract All Numbers from String in Excel

Code Breakdown

  • A User Defined Function (UDF):  Remove_Number is created.
  • CreateObject(“VBScript.RegExp”) returns matches of regular expressions and replaces them with RegEx.
  • Global is declared as TRUE to find all matches, in which the Pattern of numbers is between 0 to 9.
  • Otherwise, the Remove_Number function will be replaced with text using the Replace(Text, “”) argument.
  • Click Save in the VBA Editor window.
  • Go back to the worksheet and enter the user-defined function and use the cell reference number: B5 inside the brackets of the function.
  • The formula is:
=Remove_Number(B5)
  • Drag down the Fill Handle to see the result in the rest of the cells.

Result of Using VBA to Extract All Numbers from String in Excel


Method 4 – Separate Numbers Only from a String with Excel VBA

Consider the following dataset. To get the students’ Marks in the Data column:

Dataset for Separating Only Numbers

 

Steps:

  • Open the Visual Basic Editor in the Developer tab and Insert a Module.
  • Enter the following code in the code window.
Function Separate_Num(CellRf As String)
Dim StrgLength As Integer
StrgLength = Len(CellRf)
For x = 1 To StrgLength
If IsNumeric(Mid(CellRf, x, 1)) Then Result = Result & Mid(CellRf, x, 1)
Next x
Separate_Num = Result
End Function

Applying VBA to Separate Only Numbers from String

Code Breakdown

  • A User Defined Function (UDF): Seperate_Num is created.
  • StrgLength is declared as Integer and the number of characters is counted with the Len (CellRf) argument.
  • The IsNumeric(Mid(CellRf, x, 1)) argument returns a specific number of characters as numbers and the Result & Mid(CellRf, x, 1) gets the final output: StrgLength is x=1.
  • The Seperate_Num function returns the Result.
  • Press Ctrl + S to save the code.
  • Enter the following and press Enter.
  • Drag down the Fill Handle to see the result in the rest of the cells.
=Separate_Num(B5)

Final output of Applying VBA to Separate Only Numbers from String


Download Practice Workbook

Download the free practice Excel template.


 

<< Go Back to Separate Numbers Text | Split | Learn Excel

Get FREE Advanced Excel Exercises with Solutions!
Sanjida Ahmed
Sanjida Ahmed

Sanjida Ahmed, who graduated from Daffodil International University with a degree in Software Engineering, has worked with SOFTEKO since 2021. She has written over 100 articles on Excel & VBA and, since 2022, has worked as the Project Manager of the Excel Extension Development Project in the Software Development Department. Since starting this software development, she has established an outstanding workflow encompassing a full SDLC. She always tries to create a bridge between her skills and interests in... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo