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.
Related
My ultimate goal is to be able to search a file path, where another user will be creating a new folder each month (for example next month the user will create the folder "January 2022"), and I need to be able to search for that folder in an automated way based on the current month and year.
My plan is to create a string object like so:
Dim strDirectory As String
strDirectory = "C:\foo\bar\baz\" & strCurrentMonthName & " " & strCurrentYear
But, I'm having trouble extracting the year as text. Originally I tried to do this:
Dim TestMonth As String
Dim TestYear As String
TestYear = Year(Now)
TestMonth = GetMonthName(Format(Now, "mm"))
Problem is that testYear gets returned as 7/13/1905. This webpage shows how to fix this error in a spreadsheet, but that's not going to help my situation as the date will never be pulled from a spreadsheet (as I feel that's a very error-prone way to do things). The Year function supposedly only works on Variant, Numeric, or String expressions and I don't know how to get just the year from Now.
Keep in mind that my ability to access standard libraries is very limited, I'm using an older version of Excel (2016) and have no control over packages or updates (there are certain features I've had to do without such as MonthName() for example, I had to create my own version of that)
Format returns a Variant (String) containing an expression formatted according to instructions contained in a format expression.
So whatever format you express in Format will be returned as a string.
Public Sub Test()
Dim CurrentDate As Date
CurrentDate = Date
Dim strDirectory As String
strDirectory = "C:\foo\bar\baz\" & Format(CurrentDate, "mmm yy") '"C:\foo\bar\baz\Dec 21"
strDirectory = "C:\foo\bar\baz\" & Format(CurrentDate, "mmmm yyyy") '"C:\foo\bar\baz\December 2021"
End Sub
No need for a GetMonthName function - Format(Now(),"mmmm") returns December this month.
Figured it out:
Dim TestMonth As String
Dim TestYear As Date
testYear = Now
TestMonth = GetMonthName(Format(Now, "mm"))
Range("A1").Select
ActiveCell.FormulaR1C1 = "C:\foo\bar\baz\" & TestMonth & " " & Year(TestYear)
Output:
C:\foo\bar\baz\December 2021
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.
I have three workbooks (Book1,Book2,Book3) all saved as xlsx files. Each day, these workbooks are updated. Assume all workbooks belong to the following directory: C:\Users\abc\Documents\Example which also contains many other files (d,e,f.... etc). I need to check that Book1,Book2,Book3 are updated so that 'Date Modified' = Today's date. If date modified does not equal today's date i need the code to stop running and warn "Incorrect Date". I imagine this task will involve running a for-loop but I am quite new to VBA.
Try:
Sub test()
Dim arrFileNames As Variant
Dim i As Long
Dim strPath As String
Dim strExt As String
arrFileNames = Split("Book1,Book2,Book3", ",")
strPath = "C:\Users\marios.p\Desktop\test" & "\"
strExt = ".xlsx"
Set fs = CreateObject("Scripting.FileSystemObject")
For i = LBound(arrFileNames) To UBound(arrFileNames)
Set f = fs.GetFile(strPath & arrFileNames(i) & strExt)
If f.DateLastModified < Date Then
MsgBox "Incorrect Date"
End If
Next i
End Sub
I'm trying to open, and import values to a workbook from another workbook. Both files are in SharePoint and in the same directory.
It works as long as the target file has a specific name. But the file will have a variable start and ending in the name. 5 varying numbers + name + varying date (72262-Faktureringsplan-2018-11-16).
I've tried using * but it seems it doesn't work with SharePoint. How can I use some kind of "wildcards" for accessing files names in SharePoint?
This is the part of the code that will load the file:
Sub faktureringsplan()
Dim FileName As String
FileName = ActiveWorkbook.Path & "\72262-Faktureringsplan-2018-11-16.xlsx"
Set wb = Workbooks.Open(FileName)
ThisWorkbook.Sheets(1).Range("B10:B31").Value = wb.Sheets(1).Range("B2:B23").Value
MsgBox ("Ferdig")
wb.Close
End Sub
This code is trying to look for very specific filename with '\72262-Faktureringsplan-2018-11-16.xlsx'
on line:
FileName = ActiveWorkbook.Path & "\72262-Faktureringsplan-2018-11-16.xlsx"
The way wildcard("*") works, is that it replaces the part you want to match with anything. So a simple change from the above to
"\72262-Faktureringsplan-*.xlsx"
would return filename that has any date starting with '72262-Faktureringsplan' in your case.
You could also make use of VBA's date function to find a matching filedate that has the same date as today's date.
Format(date, "YYYY-MM-DD")
would return today's date in the format "2018-11-16" (date of this reply).
Essentially, you can do something like this:
FileName = ActiveWorkbook.Path & "\*-Faktureringsplan-" & Format(date, "YYYY-MM-DD") & ".xlsx"
This would match the following: ANYTHING-Faktureringsplan-TODAY'S_DATE.xlsx
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.