Thursday, January 26, 2017

Excel VBA - Removal of %20 characters

Sometimes when we download files, we get filename string with %20 interspersed between the name.To remove these %20, we can use the following VBA code:

Option Explicit
Sub pReplacePercent20()
    Dim strFileName As String
    Dim strFolderPath As String
    Dim FD As Object
    Dim FSO As Object
    Dim f As Object
   
    On Error GoTo ErrGate
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set FD = Application.FileDialog(msoFileDialogFolderPicker)
   
    With FD
        .AllowMultiSelect = False
        .Title = "Choose Folder"
        .Show
        If .SelectedItems.Count Then
            strFolderPath = .SelectedItems.Item(1)
        Else
            Exit Sub
        End If
    End With
   
    For Each f In FSO.GetFolder(strFolderPath).Files
        On Error Resume Next
        f.Name = Replace(f.Name, "%20", " ")
    Next
ErrGate:
If Err <> 0 Then
    MsgBox "ERROR NO. " & Err.Number
End If

End Sub

No comments:

Post a Comment

Hot Topics