ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   VB String Comparison In Excel (https://www.excelbanter.com/excel-programming/283477-vbulletin-string-comparison-excel.html)

Craig[_8_]

VB String Comparison In Excel
 
I want to program code in VB in Excel that eliminates
certain characters from a particular cell. The characters
a

- \ / [ ] ( ) * P/N (ALT) (OLD) (NEW)

There are some others, but these are the
characters/substrings I wish to eliminate. How would
something like that start out? If anyone has any tips,
please let me know. Thank you...

Craig

Chip Pearson[_2_]

VB String Comparison In Excel
 
Craig,

Try something like the following:

Dim S As String
Dim ReplaceTokens As Variant
Dim Ndx As Long
ReplaceTokens = Array("P/N", "'", "\", "/", "[", "]", "(", ")", _
"*", "(ALT)", "(OLD)", "(NEW)")
S = "\abc[]def/*" '<<<< STRING TO PROCESS
For Ndx = LBound(ReplaceTokens) To UBound(ReplaceTokens)
S = Replace(S, ReplaceTokens(Ndx), "")
Next Ndx
Debug.Print S


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"Craig" wrote in message
...
I want to program code in VB in Excel that eliminates
certain characters from a particular cell. The characters
a

- \ / [ ] ( ) * P/N (ALT) (OLD) (NEW)

There are some others, but these are the
characters/substrings I wish to eliminate. How would
something like that start out? If anyone has any tips,
please let me know. Thank you...

Craig




Dave Peterson[_3_]

VB String Comparison In Excel
 
One cell???

then I'd do something like:

Option Explicit
Sub testme()

Dim myWords As Variant
Dim iCtr As Long
Dim rng As Range
Dim myStr As String

myWords = Array("P/N", "(ALT)", "(OLD)", "(NEW)", _
"-", "\", "/", "[", "]", "(", ")", "~*")

Set rng = Worksheets("sheet1").Range("a1")

myStr = rng.Value
For iCtr = LBound(myWords) To UBound(myWords)
myStr = Application.Substitute(myStr, myWords(iCtr), "")
Next iCtr

rng.Value = myStr

End Sub

Notice that the asterisk became ~* and I rearranged the strings so that I did
P/N before the slash character and the same with the parentheses!

If you add the question mark, you'll need ~? (and same for the tilde itself ~
becomes ~~).

If you have multiple cells, it might be quicker to do a bunch of Edit|replaces
against those cells--instead of looping though each of the cells.



Craig wrote:

I want to program code in VB in Excel that eliminates
certain characters from a particular cell. The characters
a

- \ / [ ] ( ) * P/N (ALT) (OLD) (NEW)

There are some others, but these are the
characters/substrings I wish to eliminate. How would
something like that start out? If anyone has any tips,
please let me know. Thank you...

Craig


--

Dave Peterson



All times are GMT +1. The time now is 05:54 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com