VBA replace letter and number

MrTeeny

Board Regular
Joined
Jul 26, 2017
Messages
238
I'm trying to remove an occurence of a letter and number from a string, the number may or may not appear in the string, It'll be in the format T1,T2 etc in a string like so

Football T9 something else

I'd just want the string changed to

Football something else

It'd be used in a UDF if that makes any difference, I was basically looking for how to use wildcard for the number as the letter would always be T.

Thanks
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
could you not just do a find replace " T? "
 
Upvote 0
could you not just do a find replace " T? "

Thanks unfortunately that doesn't work either , I just need the regex for the numbers , taking the letter out is simple enough using

= Replace(str, " T", "") but the question mark or # don't do anything, I'm guessing they're being taken literally as a string in " T? "
 
Upvote 0
= Replace(str, " T", "") is only replacing "space T"

the question mark is a wild card hence why in find replace I was looking at space T and a character followed by space, that may not be what finally works
 
Upvote 0
Here is a UDF that you can try...
Code:
Function NoTnumber(S As String) As String
  Dim X As Long, Words() As String
  Words = Split(S)
  For X = 0 To UBound(Words)
    If Words(X) Like "T#" Then Words(X) = ""
  Next
  NoTnumber = Application.Trim(Join(Words))
End Function
 
Upvote 0
Here is a UDF that you can try...
Code:
Function NoTnumber(S As String) As String
  Dim X As Long, Words() As String
  Words = Split(S)
  For X = 0 To UBound(Words)
    If Words(X) Like "T#" Then Words(X) = ""
  Next
  NoTnumber = Application.Trim(Join(Words))
End Function
The code I posted will work no matter how many digits follow the T. If that number after the "T" is always a single digit, then you can use this formula instead...

=TRIM(REPLACE(A1,FIND(" T"," "&A1&" T"),2,""))


EDIT NOTE: If there can be more than one digit following the "T", then this longer formula should work...

=TRIM(REPLACE(A1,FIND(" T"," "&A1&" T"),FIND(" ",A1&" ",FIND(" T"," "&A1&" T")+1)-FIND(" T"," "&A1&" T"),""))
 
Last edited:
Upvote 0
= Replace(str, " T", "") is only replacing "space T"

the question mark is a wild card hence why in find replace I was looking at space T and a character followed by space, that may not be what finally works

I did try

Code:
[COLOR=#574123]= Replace(str, " T? ", "") [/COLOR]

but that didn't work , like I said I think it's taking the T? literally as those characters rather than using the ? as a wildcard.

I ended up splitting the string and passing the offending portion thru a replace statement which works fine and is a single line, thanks for the UDF Rick I'll see if I can incorporate it within my current coding
 
Upvote 0

Forum statistics

Threads
1,214,426
Messages
6,119,417
Members
448,895
Latest member
omarahmed1

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