replacing number from the text

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi
What is the best way to get rid of numbers in the column below? I only want "John" name to appear without numbers. I can do Find/Replace, or use nested Substitute() but has to be nested. Is there better way to do that? Thank you very much

john1
john2
john3
john4
john5
john6
john7
john8
john9
john10
john11
john12
john13
john14
john15
john16
john17
john18
john19
john20
john21
john22
john23
john24
john25
john26
john27

<colgroup><col width="64" style="width:48pt"> </colgroup><tbody>
</tbody>
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hi lezawang,

You can use a UDF to replace any non alphabetical characters

Code:
Function REPLACENUM(txt As String) As String
    With CreateObject("VBScript.RegExp")
        .Global = True
        .Pattern = "[^a-zA-Z]"
        REPLACENUM = .Replace(txt, "")
    End With
End Function
 
Last edited:
Upvote 0
Thank you very much for your help. Could you please break the code down. I did not understand this part
.Pattern = "[^a-zA-Z]"

Also what is UDF? Thanks once again.


You can use a UDF to replace any non alphabetical characters

Code:
Function REPLACENUM(txt As String) As String
    With CreateObject("VBScript.RegExp")
        .Global = True
        .Pattern = "[^a-zA-Z]"
        REPLACENUM = .Replace(txt, "")
    End With
End Function
 
Last edited:
Upvote 0
Thank you very much for your help. Could you please break the code down. I did not understand this part
.Pattern = "[^a-zA-Z]"

Also what is UDF? Thanks once again.

Yeah no worries, the "[^a-zA-Z]" part basically looks for any character in your string that isn't between a-z or A-Z. So e.g. "he11o w0rld" would pick up "11 0" (includes the space)
Then the code replaces all of this text with "" (blank) so in this instance "heowrld" would be the new string after calling the function.

A UDF is a User Defined Function, a function that is created by the user (your very own function if you will)

I hope this helps and solves the problem!
 
Upvote 0

Forum statistics

Threads
1,215,521
Messages
6,125,305
Members
449,218
Latest member
Excel Master

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top