Is there a way to make a macro to save a file with the current day in the name. I want to save this off everyday with the correct date.
This is what I have as a macro, pretty simple, but I am having issues with getting the current date formula in the file name (if possible)
Sub Save()
ActiveWorkbook.SaveAs Filename:="X:\file06-21-2012\.xlsm", FileFormat _
:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
End Sub
So tomorrow i would want the marco to save it as file06-22-2012.
Thanks
Like this?
Sub Save()
Dim FilePath As String
Dim NewName As String
FilePath = "X:\": NewName = FilePath & "file" & Format(Date, "MM-DD-YYYY") & ".xlsm"
ActiveWorkbook.SaveAs Filename:=NewName, FileFormat _
:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
End Sub
WIth all due respect to #HeadofCatering's answer, a simpler, more easily readable approach would be this, I think.
Sub Save()
Dim dtDate As Date
dtDate = Date
Dim strFile As String
strFile = "X:\file" & Format(dtDate, "mm-dd-yyyy") & ".xlsm"
ActiveWorkbook.SaveAs Filename:=strFile, FileFormat _
:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
End Sub
Related
I have an Excel that gets updated with sales numbers daily. At the end of the week, when the Excel is complete, I export a PDF copy of the WEEKLY worksheet. Once I have a PDF copy, the sales numbers are transferred to another sheet within the workbook, emptying the WEEKLY worksheet.
In addition to this PDF copy of the WEEKLY worksheet, I'd like to export the entire workbook in a separate Excel file to the same location (.xls format is fine). I'd like to do this before emptying the WEEKLY worksheet. I've tried using a save as macro, but I want to remain in my original Excel - not the newly saved file.
For reference, here's the VBA code for my PDF export:
Sub SaveWeekly()
'
' SaveWeekly Macro
'
'
Sheets("WEEKLY").Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"Z:\Excel New\Previous Excels\" & Range("A1") & " " & Range("H1") & ", " & Format(Date, "yyyy") & ".pdf", Quality:= _
xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End Sub
Any help is appreciated.
Try this code
Sub Test()
Dim strDate As String
Dim strTime As String
strDate = Format(Date, "DD-MM-YYYY")
strTime = Format(Time, "hh.mm.ss")
Application.DisplayAlerts = False
With ActiveWorkbook
.SaveCopyAs fileName:=ThisWorkbook.Path & "\" & strDate & "_" & strTime & "_" & .Name
End With
Application.DisplayAlerts = True
End Sub
I am trying to build a macro that will save my Excel file with the specified name (customer and date).
Not working so far and as I am not very fluent in VBA maybe someone here would be willing to help:
Sub Save()
Sheets("Tool").Unprotect Password:="xxxx"
Dim fclient As String
Dim path As String
fclient = Range("G11").Value
path = Application.ActiveWorkbook.path
fname = "Discount for " & fclient
Application.ActiveWorkbook.SaveAs Filename:=path & "\" & fname & Format(Now, "DD-MM-YYYY"), FileFormat:=52, CreateBackup:=False
Sheets("Tool").Protect Password:="xxxx"
End Sub
Try using ThisWorkbook instead of Activeworkbook
Rather than putting the Format(Now(DD-MM-YYYY)) directly into your path, you can set the date into a cell in the sheet and then use the cells value as part of the file name the same as you have done for the clients name.
The Date function uses your current system date in the DD/MM/YYYY format rather than DD/MM/YYYY HH:MM:SS which Now uses.
I've adapted this to your code along with a 'find and replace' code to find the "/" in the date and replace it with "_". (NOTE this was simply recorded and could be written better I'm sure.) You could change the underscore to any other valid character for a file name if you wish.
In my test I removed Path as if you omit a path in the file name it will use the current files path.
Sub Save()
Dim fclient As String
Dim tdate As String
Range("G12") = Date
tdate = Range("G12")
fclient = Range("G11")
Cells(12, 7).Replace What:="/", Replacement:="_", LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
tdate = Range("G12")
fname = "Discount for " & fclient
ThisWorkbook.SaveAs Filename:="\" & fname & "_" & tdate _
, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
End Sub
I've assumed cell G12 is able to contain the date.
Why your original code doesn't work i'm not sure but this is an alternative.
With no coding knowledge, I have attempted to use some code found here: Automatically name a file based on cell data when saving a spreadsheet?. Thanks to Jean-François Corbett
I have adapted as follows to show the dialog box:
Sub SaveAsString()
Dim strPath As String
Dim strFolderPath As String
strFolderPath = "N:\PROJECTS\"
strPath = strFolderPath & _
Sheet1.Range("B2").Value & "_" & _
Sheet1.Range("B6").Value & "_" & _
Sheet1.Range("X1").Value & "-JS-1" & ".xlsm"
Application.Dialogs(xlDialogSaveAs).Show strPath
ActiveWorkbook.SaveAs FileFormat:=xlOpenXMLWorkbookMacroEnabled
End Sub
I am opening an .xltm file, and attempting to save with the ability to select the subfolder of N:\PROJECTS\ with the combination of cell references shown.
The dialog box appears fine, already showing N:\PROJECTS. However, it does not fill the file name, unless the file is first saved as a .xlsm. It then always attempts to overwrite as well.
This worked for me, utilizing a slightly different code technique.
Option Explicit
Sub SaveAsString()
Dim strPath As String
Dim strFolderPath As String
ChDir "N:\PROJECTS\" 'set directory with this line
With Sheet1
strPath = .Range("B2").Value
strPath = strPath & "_" & .Range("B6").Value
strPath = strPath & "_" & .Range("X1").Value
strPath = strPath & "-JS-1.xlsm"
End With
Application.Dialogs(xlDialogSaveAs).Show strPath 'load file name with this argument
ActiveWorkbook.SaveAs FileFormat:=xlOpenXMLWorkbookMacroEnabled
End Sub
The main issue remaining was that opening from a template didn't automatically save as .xlsm. Apparently Application.Dialogs doesn't support file filters, so the problem is better solved with GetSaveasFileName.
Full code as follows:
Sub SaveAsString()
Dim strPath As String
Dim strFolderPath As String
strFolderPath = "N:\PROJECTS\"
strPath = strFolderPath & _
Sheet1.Range("B2").Value & "_" & _
Sheet1.Range("B6").Value & "_" & _
Sheet1.Range("X1").Value & "-JS-1" & ".xlsm"
fileSaveName = Application.GetSaveAsFilename(strPath _
, fileFilter:="Excel Files (*.xlsm), *.xlsm")
End Sub
I have a series of CSV files that come to me bundled in a folder simply named for the month. I've got code working to find them, open them, parse them and I'm having trouble saving them the way I want to. What I'm aiming at is saving as the same file name as it was just in the new and parsed format.
Sub OpenCSVs_2()
Dim MyFiles As String, ThisMonth As String, Convert As String
Dim startPath As String
ThisMonth = Format(Date, "mmmm")
startPath = "C:\Users\ME\Desktop\CSV find convert tests\" & ThisMonth & "\"
MyFiles = Dir(startPath & "*.csv")
Convert = Dir(startPath & "*xlsx")
Do While MyFiles <> ""
Workbooks.Open startPath & MyFiles
Call Parse1
ActiveWorkbook.SaveAs Filename:="startPath & Convert", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
MyFiles = Dir '<----------------error happens here
Loop
End Sub
The above actually does something and creates an xlsm file names "startPath & Convert". I'm sure the solution is right in front of me.
As with my previous post, you are putting your variables in quotes which then turns it into a string. so first, remove the quotes on startPath & MyFiles, then just replace the extension using the function Replace. I also added the Workbook object as you should avoid using Activeworkbook as it can cause issues.
Sub OpenCSVs_2()
Dim MyFiles As String, ThisMonth As String
Dim startPath As String
Dim wb As Workbook
ThisMonth = Format(Date, "mmmm")
startPath = "C:\Users\ME\Desktop\CSV find convert tests\" & ThisMonth & "\"
MyFiles = Dir(startPath & "*.csv")
Do While MyFiles <> ""
Set wb = Workbooks.Open(startPath & MyFiles)
Call Parse1
wb.SaveAs Filename:=startPath & Replace(MyFiles, ".csv", ".xlsx"), FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
MyFiles = Dir
Loop
End Sub
How can I save the excel file using vba code so that the Username and Date are attached in a macro?
Here is the code that I worked on to try to make it work:
ActiveWorkbook.SaveAs FileName:=(Environ$("Username")) & "_" & Date & "_BKMtracker.xlsx", FileFormat:=xlOpenXMLWorkbook
Try this:-
ActiveWorkbook.SaveAs FileName:=(Environ$("Username")) & "_" & Date & "_BKMtracker.xlsx", FileFormat:=xlOpenXMLWorkbook
With credit to #MatthewD
Sub SaveDocument()
Dim username As String
Dim nowFormated As String
Dim path As String
Dim filename As String
Dim extention As String
username = Environ("Username") & "_" 'gets the username
nowFormated = CStr(Format(now, "yymmdd")) 'or every format you like
path = "D:\" 'Wherever you want to save the file
filename = "_BKMtracker" 'or what you want
extention = ".xlsm" 'for example (with macros, else you have to change the FileFormat too)
ActiveWorkbook.SaveAs filename:=path & username & nowFormated & filname & extention, _
FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
End Sub