VBA save file with date in name

  • #1

Hi All,

I am trying to save a file with a name given in a cell D12 in sheet1 and with the current date. (eg: Report - 28/8/2014
Please let me know the code for this.

Thanks in advance.

  • #2

Hi,

Try the below code

You may need to change the path

Code:

Sub Saveas()
 
  Dim Filename

  Filename = Sheets("Sheet1").Range("D12").Value

  ActiveWorkbook.Saveas Filename:=ActiveWorkbook.Path & "/" & Filename & ".xlsx"
 
End Sub

  • #3

Hi Sathish,

But I also want the current date with the file name.

  • #4

Hi,

Try the below code

Code:

Sub Saveas()
 
  Dim Filename
  Sdate = " - " & Format(Date, "dd/mm/yyyy")
  Filename = Sheets("Sheet1").Range("D12").Value
  ActiveWorkbook.Saveas Filename:=ActiveWorkbook.Path & "/" & Filename & Sdate & ".xlsx"
 
End Sub

  • #5

Iam getting an error stating wrong number of arguments or invalid property assignment. And this error occurs in the key word "Format".

  • #6

Hi,

Try this

Note: File name will not accept "/"

Code:

Sub Saveas()
 
  Dim Filename
  Dim sdate
  sdate = " - " & VBA.Format(Date, "dd-mm-yy")
  Filename = ActiveWorkbook.Sheets("Sheet1").Range("D12").Text
  ActiveWorkbook.Saveas Filename:=ActiveWorkbook.Path & "/" & Filename & sdate & ".xlsx"
 
End Sub

  • #7

Thank you so much Sathish. This works fine...

We often need a distinct name to save our Excel files. This allows us to find our desired file among many other files. What could be more distinct than a specific date? In this article, we will discuss how to use Excel VBA to save a workbook in a specific folder with a date.


Download the Practice Workbook

You can download the practice workbook from here.


3 Easy Examples to Save Workbook in Specific Folder with Date Using Excel VBA

In this article, we will present 3 very easy VBA codes to save our Excel file as a date. Firstly, we will store our dataset as the current date. In the next one, we will conserve the Excel file as the date of the day before the current date. In the final example, we will apply a VBA to save our Excel file as the current date and time. We will use the following sample dataset to demonstrate the examples.

VBA save file with date in name


1. Saving Folder as Current Date

In this example, we will use a VBA code to save our Excel workbook as the current date. We will use the Datefunction in the VBA code to do so.

Steps:

  • Firstly, go to the Developer tab in the ribbon.
  • From there, select the Visual Basic tab.
  • Consequently, the Visual Basic window will be opened.

VBA save file with date in name

  • After that, in the Visual Basic tab, click on Insert.
  • Then, select the Module tab.
  • Consequently, a coding module will appear.

VBA save file with date in name

  • In the coding module, write down the following code.
  • Make sure to give the proper path to the folder where you want to save your workbook.

VBA save file with date in name

Sub savefileCurrentDate()
'declaring variable type
    Dim formattedCurrentDate As String
    formattedCurrentDate = Format(Date, "dd-mm-yyyy")
    Dim fileName As String
    ' assigning value to the variable
    fileName = _
"D:\The Office\save a folder with date\" & _
      formattedCurrentDate & _
      ".xlsm"  
    ActiveWorkbook.SaveAs fileName
End Sub
  • Then, save the code.

VBA save file with date in name

  • Finally, go to the Run tab and click on it.
  • From the drop-down option, select the Run command to run the code.

VBA save file with date in name

  • Consequently, you will find that your file is saved as the current date.

VBA save file with date in name

Read More:Excel VBA Save as File Format (12 Suitable Examples)


2. Saving Folder as Previous Day

In this instance, we will use a VBA code to save our Excel workbook as the previous date. We will subtract one from the Date attribute in the VBA code to do so.

Steps:

  • To begin with, go to the Developer tab in the ribbon.
  • After that, choose the Visual Basic toolbar.
  • As a result, the Visual Basic window will appear.

VBA save file with date in name

  • After that, select Insert from the Visual Basic window.
  • Next, pick Module from the menu.
  • As a result, a coding module will show up.

VBA save file with date in name

  • Write the following code in the coding module.
  • Make sure to provide the correct path to the folder where you want to save your workbook.

VBA save file with date in name

Sub savefilePreviousDate()
'declaring variable type
    Dim formattedPreviousDate As String
    formattedPreviousDate = Format(Date - 1, "dd-mm-yyyy")
    Dim fileName As String
    ' assigning value to the variable
    fileName = _
"D:\The Office\save a folder with date\" & _
      formattedPreviousDate & _
      ".xlsm"
        ActiveWorkbook.SaveAs fileName
End Sub
  • After that, save the code.

VBA save file with date in name

  • Finally, click on the Run tab.
  • To run the code, choose the Run command from the drop-down menu.

VBA save file with date in name

  • As a result, you will discover that the file is saved with the previous date.

VBA save file with date in name

Read More: How to Save Excel Macro Files as Filename from Cell Value


Similar Readings

  • Excel VBA to Save File with Variable Name (5 Examples)
  • How to Use Macro to Save Excel File with New Name (5 Ways)
  • How to Save a Copy as XLSX Using Excel VBA (5 Suitable Ways)

3. Saving Folder as Today’s Date with Time

In this demonstration, we’ll save our Excel workbook with the current date and time using a VBA script. We will use the NOW function in the VBA code to do so.

Steps:

  • First, select the Developer tab from the ribbon.
  • Then, select the Visual Basic tab.
  • The Visual Basic window will consequently be displayed.

VBA save file with date in name

  • Then, click Insert in the Visual Basic tab.
  • Next, pick Module from the options.
  • As a result, a coding module will appear on the screen.

VBA save file with date in name

  • After that, enter the following code into the coding module.
  • Make sure to give the proper path to the folder where the workbook will be saved.

VBA save file with date in name

Sub savefileWithTime()
'declaring variable type
    Dim formattedDateWithTime As String
    formattedDateWithTime = Format(Now, "dd-mm-yyyy-hh-mm-ss")
    Dim fileName As String
    'assigning value to the variable
    fileName = _
"D:\The Office\save a folder with date\" & _
      formattedDateWithTime & _
      ".xlsm"
        ActiveWorkbook.SaveAs fileName
End Sub
  • After that, save the code.

VBA save file with date in name

  • Finally, choose the Run tab.
  • From the available options, select the Run command to run the code.

VBA save file with date in name

  • Consequently, you will find that your file is saved with the current date and time.

VBA save file with date in name

Read More: Excel VBA: Save Workbook as New File in Same Folder


Conclusion

In this article, we have discussed three simple VBA codes that allow us to save our Excel workbook as a date in a specific folder. These will help the users to give their Excel files distinct names, so they can manage the files efficiently.


  • Excel VBA: Save Sheet as New Workbook without Opening
  • Excel VBA to Save as File Using Path from Cell (With Quick Steps)
  • Excel VBA: Save Workbook without a Prompt (with Easy Steps)

How to save File name with date in VBA?

Formatting today's date and time into a text string that can be used in a filename is achieved using the FORMAT function, obvious when you think of it. The format to use is as follows: Format( Now(), “DD-MMM-YY”)

How do I save a File name with date and time?

Manual Rename.
Right-click on the saved file..
Select "Rename" from the drop-down menu..
Rename the file including the date. Use dashes to separate portions of the name: the Windows operating system excludes certain characters from file names, but not dashes. If the file name is "examplefile..

How do I save a workbook with a date in VBA?

How to save file with date and time in Excel?.
Save Excel file with date and time by using VBA code..
Hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window..
Click Insert > Module, and paste the following code in the Module Window..

How to add date and time to File name in VBA?

In Excel vba we can create new file or save the current workbook with different name..
Parse File name & its extension..
Get today Date & Time..
Format as per use preference..
Add formatted Date-Time-Stamp before file extension..
Save the file..