View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
david mcritchie david mcritchie is offline
external usenet poster
 
Posts: 691
Default Shorten Code for repeated function use

Actually I think he means more like two character strings
of individual character substitutions, such usage has been
around in mainframe computing 1960's to present. Never
seen anything similar posted in VBA. PL/I and REXX it is Translate,
expect it to be same in SAS, in Assembler it is XLAT,
in COBOL it is TRANSFORM. You can actually specify only
the "to" string in REXX and the "from" string is the characters
x'00 through x'FF (Helps to translate between EBCDIC and
ASCII except that characters are not actually two way reversible).
http://mitvma.mit.edu/cmshelp.cgi?RE...RANSLAT%20(ALL
http://www.scoug.com/OPENHOUSE/REXXI...BISTTR1.4.HTML

You would have to write your own User Defined Function.

newString = translate(oldString, toString, fromString)

Function translate(oldString As String, toString As String, _
fromString As String) As String
'David McRitchie, 2005-03-08 programing
' http://groups.google.com/groups?thre...0a% 40phx.gbl
'limited to equal length from and to strings for now
'ref. http://mitvma.mit.edu/cmshelp.cgi?RE...RANSLAT%20(ALL
Dim i As Long, pos As Long, str As String
str = ""
For i = 1 To Len(oldString)
'-- InStr([start, ]haystack, needle[, compare])
pos = InStr(1, fromString, Mid(oldString, i, 1), vbBinaryCompare)
If pos = 0 Then
str = str & Mid(oldString, i, 1)
Else
str = str & Mid(toString, i, 1)
End If
Next i
translate = str
End Function
--
HTH,
David McRitchie, Microsoft MVP - Excel [site changed Nov. 2001]
My Excel Pages: http://www.mvps.org/dmcritchie/excel/excel.htm
Search Page: http://www.mvps.org/dmcritchie/excel/search.htm

"Bob Phillips" wrote in message ...
Do you mean like

myArray = Array ("&","/","\","^")

For i = LBound(myArray) To UBound(myArray)
ReplNameChar = Application.WorksheetFunction.Substitute
(ReplNameChar, myArray(i), "_and_") ' remove &
Next i


--
HTH

Bob Phillips

"mel" wrote in message
...
I use the following command to replace characters in a
string which are not allowed. I use this command up to 30
times to remove all of the unwanted characters. Is there
a way to set up an array and get the function to work
with one value after another in the array rather than
having a separate statement for each occurrence?

ReplNameChar = Application.WorksheetFunction.Substitute
(ReplNameChar, "&", "_and_") ' remove &

TIA