Save file to a new name using excel vba

OALes

New Member
Joined
May 29, 2024
Messages
7
Office Version
  1. 2016
Platform
  1. Windows
  2. MacOS
Hello, and thank you for your help!
I need to add a section to my sub in excel 2016 vba that will go to a new folder and name my new file based on a preset naming confvention changing the date included as the previous date.

Every morning I export a file which tells us what we want to know about a certain subject. I have a macro that reformats, adds new information, sets a print area and margins, now I need to save it. This file goes in the same folder every day and is named the same thing except for the date. I need to use the previous date because it refers to the data collected on that day.

I would like to move to the appropriate folder and name the file using an input box to set the date in question.

Can you help?
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Hi @OALes

You did not provide the folder in which the workbook should be saved and the date format.
The below VBA code will save a copy of your current (active) Workbook to the same location adding the date you specified as suffix (formatted as -YYYYMMDD).


VBA Code:
Option Explicit

Sub SaveWithDate()
   Dim fPath As String: fPath = Application.ActiveWorkbook.Path
   Dim fName As String: fName = Application.ActiveWorkbook.Name
   Dim fDate As String
   Dim fExtn As String
   Dim fNnew As String
   Dim DateStr As String
   Dim d As Date
   
   Do Until IsDate(DateStr)
      DateStr = InputBox("MM/DD/YYYY", "Please enter a date")
   Loop
   d = CDate(DateStr)
   fDate = Format(d, "yyyymmdd")
   fExtn = Right(fName, Len(fName) - InStrRev(fName, ".") + 1)
   fName = Left(fName, InStr(fName, ".") - 1)
   fNnew = fPath & "\" & fName & "-" & fDate & fExtn
   Application.ActiveWorkbook.SaveCopyAs FileNAME:=fNnew
End Sub

Please let me know the above stated details and I'll edit the code to suit your needs.
 
Upvote 0

Forum statistics

Threads
1,224,811
Messages
6,181,082
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top