I have written a below mentioned code which export the Query into a Excel file.
Public Function exportToXl11()
Dim dbs As DAO.Database
Set dbs = CurrentDb
Dim sFilename As String
sFilename = "c:\ExportFile\Output_Results.xlsx"
DoCmd.OutputTo acOutputQuery, "metrics2", acFormatXLSX, sFilename, Autostart:=False
End Function
I have another table name "Date1" which have date. I want a code which will export the query with a date given in a "Date1" table. Like the output will be "Output_results_23-05-2020.xlsx" instead of "Output_Results.xlsx"
You can include the date in the filename variable:
sFilename = "c:\ExportFile\Output_Results_" & Format$(Now, "yyyymmdd") & ".xlsx"
You can format it any way you want, but make sure you do not include characters that are not allowed in filenames.
Related
I'm creating an excel file from nothing, adding content and saving it. I want to rename the excel file once I saved it, using VBA code. The file I want to rename isn't the same file in which I'm writing the code.
Currently I'm trying to do it this way (this is a snippet of my code, just to show how I'm saving the file):
Dim workbook1 As Workbook
Dim name As String, lastcell As String
Dim oldname As String, newname As String
Set workbook1 = Application.Workbooks.Add
name = "financial report - "
workbook1.SaveAs ThisWorkbook.Path & "\" & name & ".xlsx"
'lastcell has a date that I want in my new title
lastcell = Range("A1").End(xlDown).Text
oldname = ThisWorkbook.Path & "\" & name & ".xlsx"
newname = ThisWorkbook.Path & "\" & name & lastcell & ".xlsx"
Name oldname As newname
But when I run it I get this:
The value in my lastcell variable is supposed to be in a date format like this dd/mm/yyyy. The exact cell I'm trying to copy and use as part of the name of my new excel is 05/02/2021.
The value in name by the end of the sub should be financial report - 05/02/2021.
I'm gonna be surprised if this hasn't been asked before. Does anybody know what I'm doing wrong or have any recomendations for my code?
You need to provide a date value which can work as part of a filename:
lastcell = Format(Range("A1").End(xlDown),"yyyy-mm-dd")
Check out
https://learn.microsoft.com/en-us/office/vba/api/excel.workbook.savecopyas
workbook1.SaveCopyAs newname
There you have your issue 05/02/2021 cannot be part of the file name as a slash / is not allowed in file names. Slash and backslash are considered to be path seperators.
Try the following: Make sure the variables are declared properly as below, to ensure the date is read as numerical date and not as some text that cannot be formatted.
Dim Name As String
Name = "financial report - "
Dim ReportDate As Date
ReportDate = Range("A1").End(xlDown).Value 'make sure you read the `.Value` not `.Text`
workbook1.SaveAs ThisWorkbook.Path & "\" & Name & ".xlsx"
workbook1.SaveCopyAs ThisWorkbook.Path & "\" & Name & Format$(ReportDate, "yyyy-mm-dd") ".xlsx"
Also use .SaveAs to save the original workbook and .SaveCopyAs to save the copy with the date attached.
We have a daily report sending to a folder. The next day, our coordinator generates a summary by data query connecting the most recently updated file into the master file.
The file has a time stamp. We could not find a way to incorporate the time stamp into the Excel built-in data query.
I'm using Excel VBA to find the most recently updated report and change the file name into a standard name.
Sub ChangeName()
Dim folderPath As String, tableName As String, latestTblName As String
Dim latestModified As Date, modifiedDate As Date
folderPath = " F:\Purchasing\Supply Chain Reports\IMAP Reports\"
tableName = Dir(folderPath & "*.xlsx")
Do While tableName <> vbNullString
modifiedDate = FileDateTime(folderPath & tableName)
If latestModified < modifiedDate Then
latestModified = modifiedDate
latestTblName = tableName
End If
tableName = Dir()
Loop
Workbooks.Open folderPath & latestTblName
Rows("0:6").Delete
Kill "F:\Purchasing\Supply Chain Reports\IMAP Reports\ InvoiceQueryReport.xlsx"
Dim thisWb As Workbook
Set thisWb = ActiveWorkbook
MyOldName = ActiveWorkbook.FullName
MyNewName = "InvoiceQueryReport.xlsx" & ActiveWorkbook.Name
ActiveWorkbook.SaveAs Filename:="F:\Purchasing\Supply Chain Reports\IMAP Reports\" & MyNewName
Kill MyOldName
End Sub
I had an error in the TableName, also when it saved, the master file was saved as the standard name, instead of the report.
I'm currently trying to get an excel file to save into YYYYMMDD_fixed name piece_INITIALS OF LAST PERSON TO EDIT.
I'm using Environ function to call the User's and PC's name in a cell that i've found can be used to add to the name.
The issues i'm trying to fix are:
how can i define the save path to work on any PC regardless of user name, as current path has Users/my name/ , and up to 4 people with different PCs and names will edit this file. it should just save on Desktop on any of the 4 PCs
how can i modify the
strFile = "C:\Users\me\Desktop\" & Format(dtDate, "ddmmyyyy") & ".xlsx"
part so that it displays YYYYMMDD_name (i get this part ok) _ABC where ABC value is in cell A1 generated by the below attr function?
the function used is
Function attr(choice) As String
Select Case (choice)
Case "computer": attr = Environ("Computername")
Case "user": attr = Environ("UserName")
End Select
End Function
and the one i use to save (albeit a different format on a different file) is
Dim dtDate As Date
dtDate = Date
Dim strFile As String
strFile = "C:\Users\me\Desktop\" & Format(dtDate, "ddmmyyyy") & ".xlsx"
ActiveWorkbook.SaveAs Filename:=strFile, FileFormat _
:=51, CreateBackup:=False
Any help would be greatly appreciated!
Programming is not my main job, just trying to automate bits where possible, so go easy on me please :)
Maybe something like that will help:
Dim strFile As String, strUserName As String
Dim dtDate As Date
dtDate = Now
strUserName = attr("user")
strFile = "C:\Users\" & strUserName & "\Desktop\" & Format(dtDate, "ddmmyyyy") & "_" & Sheets("Sheet1").Range("A1").Value & ".xlsx"
MsgBox strFile
Note that I assigned the value of an active username to strUserName and I'm using it with your strFile. I also added Sheets("Sheet1").Range("A1").Value to the code (change sheet name accordingly). The final result will look like that:
C:\Users\username\Desktop\12082019_username.xlsx
I am trying to set today's date and use it in different formats for various tasks, but I have no idea how to go about it. I know that Date is a global variable but it is in 2017-12-19 format on our system, for some tasks I'd need it to be mm-dd-yyyy or just mm-dd but I don't know how could I set this up without prompting the user to manually enter the date in said formats. Also can I use the Date variable just like a string or do I need to convert it?
Sub BasedOnDate()
Dim filename As String, Source As String, Destination As String
Dim myValx As String
Dim myVal1 As Date
Dim myValn As Date
Dim myFile As String
Dim myPath As String
'trying to declare today's date, but just the month and day as "mm-yy"
myVal1 = Date("mm-yy")
myValn = Replace(myVal1, "-", "\")
'trying to declare the date here in the desired format
myValx = Date("mm-dd-yyyy")
'File paths
Source = "\\Rdcicgtcuwd01p\app_log\36196_WMS\WMS_36196_PROD_" & myValx & ".csv"
Destination = "\\olscmesf003\gcm_emea\TCU_REPORTS\APPS\Reports\Regional\Workflow Management System\2017\" & myValn
FileCopy Source, Destination
End Sub
You could use Format() function:
myValx = Format(Date, "mm-dd-yyyy")
Tip: Please keep in mind that Format() returns string, here it works fine, but could be a problem in other situation.
I am trying to import files from a folder which is generated on the date the user specifies.
The path looks something like this:
\\sample\example_group\xxx_REPORTS\APPS\Reports\Regional\APP NAME\ and after the last "\" there is today's date in yyyy/mm/dd format.
The folder name doesn't contain slash. It's the folder structure that is generated every day app\year\month\day so it would look something like this: \APP NAME\2017" & "\" & "myVal1" & "\" & "nyVal" & "\"
I am trying to prompt the user to input the date he's reviewing a report for and then have Excel open that folder.
I have the following code, but it doesn't take the user input into account.
Sub ImportFile()
Dim dFile As FileDialog, result As Integer, it As Variant
Dim myDate As String
Dim myval2 As Variant
myval2 = InputBox("Enter today's date in yyyy/mm/dd format")
myDate = Format(Date, "yyyy/mm/dd")
Set dFile = Application.FileDialog(msoFileDialogOpen)
dFile.InitialFileName = "\\sample\example_group\xxx_REPORTS\APPS\Reports\Regional\APP NAME\" & "myval2"
If dFile.Show = -1 Then
Debug.Print dFile.SelectedItems(1)
End If
End Sub
It does not take the user's input into account, because you pass the variable name myval2 as a string. Thus, it should be like this:
dFile.InitialFileName = "\gional\APP NAME\" & myval2
in stead of:
dFile.InitialFileName = "\gional\APP NAME\" & "myval2"
A folder name can't contain a forward slash (/) and therefore your approach won't work. I suggest using the format yyyy-mm-dd instead.