• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Rename file extension

ashish mehra

Active Member
Hi all,

I have a list of files saved on desktop in a specific folder in .csv & .xls format. I want to change the name & format to .xlsx

I try with following code:

Code:
Sub TEST()

Dim wbCounter As Integer
Dim wbPath As String
Dim newFileName As String
Dim myPath As String
Dim newfilenamewithoutext As String

myPath = "C:\Ashish\"


With Application.FileDialog(msoFileDialogOpen)

    .AllowMultiSelect = True
    .Title = "Select Files to be renamed"
    .Show
  
    wbCounter = .SelectedItems.Count
  
    For i = 1 To wbCounter
      
        wbPath = .SelectedItems(i)
        Workbooks.Open (wbPath)
        newFileName = ActiveWorkbook.Name
        ActiveWorkbook.SaveAs Filename:= _
        myPath & "newfilenamewithoutext" & ".xlsx", FileFormat:= _
        xlOpenXMLWorkbook, CreateBackup:=False
        ActiveWindow.Close
      
    Next i

End With

End Sub

Regards,
AM
 
Name must get changed for each new file else that will replace the existing the one or macro stop after throwing error.

Code:
Sub TEST()

Dim wbCounter As Integer
Dim wbPath As String
Dim newFileName As String
Dim myPath As String
Dim newfilenamewithoutext As String

myPath = "C:\Ashish\"


With Application.FileDialog(msoFileDialogOpen)

    .AllowMultiSelect = True
    .Title = "Select Files to be renamed"
    .Show
 
    wbCounter = .SelectedItems.Count
 
    For i = 1 To wbCounter
     
        wbPath = .SelectedItems(i)
        Workbooks.Open (wbPath)
      '  newFileName = ActiveWorkbook.Name & "_newfilenamewithoutext"
        'or
        newFileName = "newfilenamewithoutext" & i
     
        ActiveWorkbook.SaveAs Filename:= _
        myPath & newFileName & ".xlsx", FileFormat:= _
        xlOpenXMLWorkbook, CreateBackup:=False
        ActiveWindow.Close
     
    Next i

End With

End Sub
 
Back
Top