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
Related
Another program sends a date format in the computer's local language to excel, i.e ÅÅÅÅ-MM-DD
VBA can't interpret this because the Format function only supports date symbols in English.
I can only retrieve localized date symbols from the local language, like ÅÅÅÅ-MM-DD. the format function only accept English localized date symbols YYYY-MM-DD. I need a function that can interpret date format in local system language or is able to translate ÅÅÅÅ-MM-DD with code to YYYY-MM-DD.
More examples:
DD.MM.ÅÅÅÅ to DD.MM.YYYY (local system language Swedish to English)
MM.DD.ÅÅÅÅ to MM.DD.YYYY (local system language Swedish to English)
JJ.MM.AAAA to DD.MM.YYYY (local system language French to English)
DD.MM.RRRR to DD.MM.YYYY (local system language Polish to English)
The workaround I've come up with uses TEXT formula to interpret the local system languages date format.
This *should* work with different local system languages:
Sub tstLocalDate()
Dim yrCode As String
Dim mnthCode As String
Dim dyCode As String
Dim dateFormat As String
yrCode = WorksheetFunction.Rept(Application.International(xlYearCode), 4)
mnthCode = WorksheetFunction.Rept(Application.International(xlMonthCode), 2)
dyCode = WorksheetFunction.Rept(Application.International(xlDayCode), 2)
dateFormat = yrCode & "-" & mnthCode & "-" & dyCode
Msgbox LocalDateFormat(DateValue(Now()), dateFormat)
End Sub
Function LocalDateFormat(inputDate As String, inputLocalFormat As String)
Dim LocalTextFormula As String
With ThisWorkbook.Sheets(Meta.Name).Range("A3")
.Formula = "=text("""",""?"")"
LocalTextFormula = Left(.FormulaLocal, InStr(1, .FormulaLocal, "("))
.FormulaLocal = LocalTextFormula & """" & inputDate & """;""" & inputLocalFormat & """)"
LocalDateFormat = .Value
End With
End Function
I've no idea how vulnerable this workaround is. If the text formula uses the systems local language to interpret date formats it should work for most cases, but if it's based on used excel language it won't work.
Is it possible to solve this without using excel formulas?
I guess you don't need all this.
Dim inputDate As String
Dim trueDate As Date
inputDate = "2021-12-31"
trueDate = DateValue(inputDate)
ThisWorkbook.Sheets(Meta.Name).Range("A2") = trueDate
Then apply the date format you prefer to the A2 range.
I'm trying to have the current date recognized in Excel using an =TEXT(TODAY(),"mmmm d yyyy") function, but I have run into issues with Canadian users based in Quebec, who may have a system install set to English or French, and likewise an Office install in one language or the other. For today's date, my spreadsheet pulls March 10 2021 on my computer, but the result has been askew for each Quebec-based user I have tested with.
In order to resolve this, I set up a VBA function to pull the application's language setting, with the intention to use a simple IF to check the language code and change from "mmmm d yyyy" to "mmmm j aaaa" (the letters to match the French journée for day and année for year, as per consulting the formats available on French Excel's TEXTE function). My function worked... but the output has come back incorrect each time.
Public Function LangCheck()
Dim lang_code As Long
lang_code = Application.LanguageSettings.LanguageID(msoLanguageIDUI)
LangCheck = lang_code
End Function
So that did its job, but the outputs should have been March 10 2021 or mars 10 2021. Instead from two different users I have received:
March j Wednesday, from a user with a French Excel setting, confirmed by the code outputting language ID 1036.
mars d yyyy, from a user with an English Excel setting on a French OS, confirmed by the code outputting language ID 1033.
Excel is translating every function to its French cognate, but won't translate the formats for the TEXT function on its own. Does anyone have a suggestion for a VBA or function-based solve to this conundrum so that I can ensure consistent dates return regardless of language?
You can use the Application.International properties to extract the correct DMY tokens, and use a named reference for the text function format code:
Change your formula to:
=TEXT(TODAY(),"dtFormat")
And store the below code into the ThisWorkbook module
Wherever the file is opened, the Sub will run and ensure that the dtFormat name has the appropriate DMY codes for the locale.
Option Explicit
'change text function date code
Private Sub Workbook_Open()
Dim yrCode As String, mnthCode As String, dyCode As String
Dim dtCode As String
Dim nM As Name
With Application
yrCode = WorksheetFunction.Rept(.International(xlYearCode), 4)
mnthCode = WorksheetFunction.Rept(.International(xlMonthCode), 4)
dyCode = WorksheetFunction.Rept(.International(xlDayCode), 1)
End With
'Can only add a name if it is absent
For Each nM In ThisWorkbook.Names
If nM.Name = "dtFormat" Then
nM.Delete
Exit For
End If
Next nM
dtCode = mnthCode & " " & dyCode & " " & yrCode
ThisWorkbook.Names.Add _
Name:="dtFormat", _
RefersTo:="=""" & dtCode & """", _
Visible:=False
End Sub
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.
Disclaimer: I've read the relevant questions/answers regarding Error 1004 and I couldn't find a solution.
I have a macro that I distributed within the team and it works perfectly on all computers (including mine) except one. She's running the same version of Excel on the same system and she can use other macros except this one, I get the "Error 1004 SaveAs function failed" message. It is by a very long shot but did I miss some security setting or is there some problem with the code that could cause this? This is the code in question:
Sub PSSaveFile()
Dim myVal2 As Variant
Dim myValn2 As String
Dim myDate As String
Dim mFilePath As String
myVal2 = InputBox("Please enter today's date in mm-dd format")
myValn2 = Replace(myVal2, "-", "\")
myDate = Date
mFilePath = "\\xxxxxxxx003\xxx_emea\TCU_REPORTS\APPS\Reports\Regional\xxxxx for PC Web xx\2017\" & myValn2
ActiveWorkbook.SaveAs FileName:=mFilePath & "\xxxRHLogs-" & myDate & "_checked"
End Sub
Let me guess - the guy with the other computer is from another country, thus using his own regional settings on the PC. Thus the date format is a bit fancier, e.g. dd/mm/yyyy and it is not allowed to save it as a file because of this.
Try to change the date like this:
myDate = year(date) & month(date) & day(date)
and try again. This would eliminate the fancy settings part.
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.