VBA String

  • Thread starter Thread starter The Skydiver
  • Start date Start date
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 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.
 
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, "*", "")
 
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).
 
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]=-
 
Thank you very much for your Help... :o)


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]=-
 
Back
Top