VBA String

T

The Skydiver

How can one remove the following characters out of a VBA string so It can be
used for naming a file:
\ / : * ? " < > |

Thank you

-=[JAV]=-
 
A

Al Edlund

a simple loop testing for them and removing them from the string
i.e. one string in, one string out, if char(x) <> one of the bad ones copy
it to string out.
 
D

Douglas J. Steele

Depending on what version of VBA you're using, you should be able to use the
Replace function repeatedly.

strName = Replace(Replace(Replace(Replace(strName, "\", ""),"/", ""), ":",
""), "*", "")

or

strName = Replace(strName, "\", "")
strName = Replace(strName, "/", "")
strName = Replace(strName, ":", "")
strName = Replace(strName, "*", "")
 
W

Wayne Morgan

Try the Replace function to replace them with "".

Public Function ReplaceCharacters(ByVal strIn As String) As String
Dim strCheck As String, i As Integer
strCheck = "\/:*?""<>|"
For i = 1 To Len(strCheck)
strIn = Replace(strIn, Mid$(strCheck, i, 1), "")
Next i
ReplaceCharacters = strIn
End Function

The two double quotes are needed in the middle of strCheck. If you only use
a single double quote here, VBA will think that it has reached the end of
the string. Doubling up the quotes tells VBA to place a quote in the string.
The "first" character is not a capital V, it is a back slash and forward
slash (2 characters).
 
T

The Skydiver

Thank you very much


Wayne Morgan said:
Try the Replace function to replace them with "".

Public Function ReplaceCharacters(ByVal strIn As String) As String
Dim strCheck As String, i As Integer
strCheck = "\/:*?""<>|"
For i = 1 To Len(strCheck)
strIn = Replace(strIn, Mid$(strCheck, i, 1), "")
Next i
ReplaceCharacters = strIn
End Function

The two double quotes are needed in the middle of strCheck. If you only use
a single double quote here, VBA will think that it has reached the end of
the string. Doubling up the quotes tells VBA to place a quote in the string.
The "first" character is not a capital V, it is a back slash and forward
slash (2 characters).

--
Wayne Morgan
Microsoft Access MVP


The Skydiver said:
How can one remove the following characters out of a VBA string so It
can
be
used for naming a file:
\ / : * ? " < > |

Thank you

-=[JAV]=-
 
T

The Skydiver

Thank you very much for your Help... :eek:)


Al Edlund said:
a simple loop testing for them and removing them from the string
i.e. one string in, one string out, if char(x) <> one of the bad ones copy
it to string out.

The Skydiver said:
How can one remove the following characters out of a VBA string so It
can
be
used for naming a file:
\ / : * ? " < > |

Thank you

-=[JAV]=-
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top