Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
mel mel is offline
external usenet poster
 
Posts: 6
Default Shorten Code for repeated function use

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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,120
Default Shorten Code for repeated function use

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



  #3   Report Post  
Posted to microsoft.public.excel.programming
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





  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 691
Default Shorten Code for repeated function use

sorry code correction is:

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, pos, 1) '-- corrected
End If
Next i
translate = str
End Function


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 691
Default Shorten Code for repeated function use

Seeing that the archived copy of the macro code is messed up on
Groups-beta-google.com I have the created a webpage for the macro.

TRANSLATE User Defined Function
"Simulate a mainframe type TRANSLATE function"
http://www.mvps.org/dmcritchie/excel/translate.htm


I have also put in some comments relating to the mess created by
groups-beta-google back in Nov. and how it keeps getting worse. The
code structure appears okay if you look at the original text in google
groups or in the actual thread in your newsbrowser, but it messed up
for those that look at Google Groups.

---
David McRitchie, Microsoft MVP - Excel
My Excel Pages: http://www.mvps.org/dmcritchie/excel/excel.htm

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Which function will shorten IF-THEN-ELSE ? Jack Ryan Excel Worksheet Functions 5 May 19th 10 09:03 PM
Which function will shorten IF-THEN-ELSE ? Jack Ryan Excel Worksheet Functions 0 May 18th 10 10:02 AM
function for repeated cells Haz Excel Worksheet Functions 7 June 3rd 07 08:56 PM
Printing text in a repeated cell/row that is longer than repeated Valerie Dyet Excel Discussion (Misc queries) 1 February 13th 06 03:27 AM
Shorten Code? Rockee052[_23_] Excel Programming 5 January 28th 04 12:49 AM


All times are GMT +1. The time now is 03:54 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"