Wednesday, May 27, 2020

Excel VBA Function to remove Invalid Characters from file or folder name

Function InvalidCharsRemover() As String

    Dim chars As Variant
    Dim x As Integer
    Dim strfilename As String

    chars = Array("<", ">", "/", "\", "?", "|", ":", ";", "*")

    strfilename = "Sales<figure>in/year\2020-21%"
    Debug.Print strfilename

    For x = LBound(chars) To UBound(chars)
        If InStr(1, strfilename, chars(x), vbBinaryCompare) <> 0 Then
            strfilename = VBA.Replace(strfilename, chars(x), "")
        End If
    Next

    InvalidCharsRemover = strfilename
    Debug.Print strfilename

End Function

Source file:- click to download.

No comments:

Post a Comment

Hot Topics