Search code examples
excelvbafile-renamechdirfileopendialog

Saving file as same name but attaching date - referencing cell date


I'm able to run this code without any error, however is giving a slightly different objective.

It'll be added below the Do Events section according to the code in the link below.

Loop through all excel file in given folder

Objective of code:

  • Browse folder for workbook in folder with date referencing to a range "H2" of current workbook and saving them in the same folder as file browsed

Current Situation:

  • Able to save file with date however it get saved in its earlier folder

E.g File exist in

C:\Users\Tyler\Desktop\Test

New file with date will be saved in

C:\Users\Tyler\Desktop

Codes

Dim CellDate As String, fName As String

    fName = Left(ActiveWorkbook.Name, (InStrRev(ActiveWorkbook.Name, ".", -1, vbTextCompare) - 1))



    CellDate = ThisWorkbook.Worksheets("Sheet1").Range("H2")

    CellDate = Format(Date, "YYYYMMDD")


    ActiveWorkbook.SaveAs fName & "-" & CellDate, FileFormat:=xlOpenXMLWorkbookMacroEnabled

Appreciate the help (:


Solution

  • Have you tried

    ActiveWorkbook.SaveAs myPath & fName & "-" & CellDate, FileFormat:=xlOpenXMLWorkbookMacroEnabled
    

    If your code for folder selection is as per the link then its value will be stored in myPath.

    Assuming "C:\Users\Tyler\Desktop\Test" was the folder selected.

    Note:

    Associated code from link....

    'Retrieve Target Folder Path From User
      Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
    
        With FldrPicker
          .Title = "Select A Target Folder"
          .AllowMultiSelect = False
            If .Show <> -1 Then GoTo NextCode
            myPath = .SelectedItems(1) & "\"
        End With