MoveExcelFile: Moves an Excel File

This microservice takes the following approach to move an Excel file:

  1. Opens an Excel file (the “source file”).
  2. Saves it as a new file (the “destination file”).
  3. Deletes the source file.

This is handy if you download a report as a CSV but want to save it as an XLSX or vice versa.

MoveExcelFile

Sub MoveExcelFile(PathToSource As String, PathToDestination As String, Optional FileType As Long = 51)

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Dim SourceWorkbook As Workbook: Set SourceWorkbook = Workbooks.Open(PathToSource, False, True)
    SourceWorkbook.SaveAs PathToDestination, FileType
    SourceWorkbook.Close SaveChanges:=False
    Kill PathToSource
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True

End Sub
ParameterDescription
PathToSourceThis is the existing Excel file (the “source file”) you would like to move.
DO include the file extension, e.g.:
“C:\Users\AHavas\Downloads\My Draft Report.csv”
PathToDestinationThis is where you would like to move the source file to.
DO NOT include the file extension, e.g.:
“C:\Users\AHavas\Downloads\My Final Report”
FIleTypeAn optional file type. Valid options include:
xlOpenXMLWorkbook – .xlsx – Excel Workbook – (DEFAULT – you can leave this parameter blank and destination file will be saved as an .xlsx)
xlOpenXMLWorkbookMacroEnabled – .xlsm – Excel Macro-Enabled Workbook
xlExcel12 – .xlsb – Excel Binary Workbook
xlCSV – .csv – CSV (Comma delimited)

Examples

MoveExcelFile "C:\Users\AHavas\Downloads\Report Alpha.xlsx", "C:\Users\AHavas\Downloads\Report Alpha", xlCSV

This will do the following:

  1. Opens the Excel file named Report Alpha.xlsx at C:\Users\AHavas\Downloads\
  2. Saves it as Report Alpha in the CSV format at C:\Users\AHavas\Downloads\
  3. Deletes C:\Users\AHavas\Downloads\Report Alpha.xlsx
Scroll to Top