VBA code to strip # sign

HWL

Active Member
Joined
Dec 1, 2009
Messages
462
I have code I made to strip all letters from a cell. I made a user function called "letterout" -- see below, however I also want to strip the pound sign out of the string.

Function LetterOut(rng As Range)
Dim i As Integer
For i = 1 To Len(rng)
Select Case Asc(Mid(rng.Value, i, 1))
Case 0 To 64, 123 To 197
LetterOut = LetterOut & Mid(rng.Value, i, 1)
End Select
Next i
End Function
I imagine it is another Case number but have no idea which. Help?
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Here is another function that will do what you want...
Code:
Function LettersOut(rng As Range) As String
    Dim X As Long
    Const CharactersToRemove As String = "[A-Za-z#]"
    LettersOut = rng.Value
    For X = 1 To Len(LettersOut)
        If Mid(LettersOut, X, 1) Like CharactersToRemove Then Mid(LettersOut, X) = Chr(1)
    Next
    LettersOut = Replace(LettersOut, Chr(1), "")
End Function
You can control the characters to remove using the CharactersToRemove constant (the Const statement)... the list using dashes to indicate ranges coupled with individual characters (see the help files for the VB "Like Operator" for all the rules on how to specify the characters.
 
Upvote 0
Here is another function that will do what you want...
Code:
Function LettersOut(rng As Range) As String
    Dim X As Long
    Const CharactersToRemove As String = "[A-Za-z#]"
    LettersOut = rng.Value
    For X = 1 To Len(LettersOut)
        If Mid(LettersOut, X, 1) Like CharactersToRemove Then Mid(LettersOut, X) = Chr(1)
    Next
    LettersOut = Replace(LettersOut, Chr(1), "")
End Function
You can control the characters to remove using the CharactersToRemove constant (the Const statement)... the list using dashes to indicate ranges coupled with individual characters (see the help files for the VB "Like Operator" for all the rules on how to specify the characters.

I used the code exactly as you posted but the # is still there. Do I need to do more?
 
Upvote 0
I used the code exactly as you posted but the # is still there. Do I need to do more?
In my tests, the function removes letters and the # sign. Can you send your workbook to me so I can see what is going on directly? My email address is rickDOTnewsATverizonDOTnet (just replace the upper case letters with the symbols they spell out).
 
Upvote 0
Yes, but 0-64 are the ones you collect in the string you're composing... ;-)

Hmm, now I tried 48 to 57 which from what I can see should be only numbers, but I'm still getting the #. Should I be using the DEX of HX column and do I need to reset the function some how to take effect?
 
Upvote 0
In my tests, the function removes letters and the # sign. Can you send your workbook to me so I can see what is going on directly? My email address is rickDOTnewsATverizonDOTnet (just replace the upper case letters with the symbols they spell out).

Ah, now I see what happened, you changed the function name to letterSout when it was originally letterout. Your's makes more sense and now works correct. Thanks for the offer to look at the code! :laugh:
 
Upvote 0
Ah, now I see what happened, you changed the function name to letterSout when it was originally letterout.
I'm sorry... I did not notice you called the function by a singular name... I thought I read it as being plural (because you were removing more than one character). I'm glad you spotted that. Again, sorry for doing that to you.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,286
Members
452,902
Latest member
Knuddeluff

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