Regional Date format in excel VBA - excel

I have a strange issues with date formatting - I am formatting dates as per below consistently - trying to get and display file details, below works fine with ldn / other settings.
However, one of the user got French date time setting, and below code fails format datetime and file size.
Not sure what is the best way to handle all regions? Any suggestions pls?
invalid FR format - 02-d꣭2019 15:23:10 , fileSize = 110,00 , instead
of 110.00
fileSize = FileLen(tool) / 1024
creationDate = Format(Local2GMT(.getFile(tool).DateCreated), "DD-MMM-YYYY HH:MM:SS")
modifiedDate = Format(Local2GMT(.getFile(tool).DateLastModified), "DD-MMM-YYYY HH:MM:SS")
accessfileDate = Format(Local2GMT(.getFile(tool).DateLastAccessed), "DD-MMM-YYYY HH:MM:SS")
Function Local2GMT(dtLocalDate As Date) As Date
Local2GMT = DateAdd("s", -GetLocalToGMTDifference(), dtLocalDate)
End Function

Related

vb.net - Formatting a datetime column of a datatable to a yyyy/MM/dd HH:mm string

I dont seem to be able to figure out what is wront in the code. Here is a screenshot to see the error message, the code, and the content of the variable.
and a bigger part of the code:
For Each value As DataColumn In dt2.Columns
If value.DataType = System.Type.GetType("System.DateTime") Then
dq = dq & value.ColumnName & " = '" & r2.Item(value.ColumnName).ToString("yyyy/MM/dd HH:mm:ss") & "' and "
End If
Next
what am I doing wrong?
That just compiles because you have Option Strict Off(no-go). You have to cast the Object returned from r2.Item(value.ColumnName) to a Date. This also fixes this issue, you can use Field:
r2.Field(Of Date)(value.ColumnName).ToString("yyyy/MM/dd HH:mm:ss")
Since you are german, note that your format string might not give you what you expect but something like 2021.02.23 15:00:24. If you want 2021/02/23 15:00:24 you need to use ToString("yyyy/MM/dd HH:mm:ss", CultureInfo.InvariantCulture). See: here

Now() is giving an error

I am getting an error in the below code.
Dim CurrentTime As Date
CurrentTime = Format(Now(), "hh:mm AM/PM")
If CurrentTime = ActiveSheet.Range("I1") Then
Call SendEMail
End If
When the time is right, then the macro is debugging and Now is highlighted.
Could anyone solve this problem?
You are not getting an actual error are you? It is just not working as expected.
Matt Cremeens has identified your issue I believe, you have declared CurrentTime as a date data type. A date data type stores a number representing a time/date, however you are asking for it to store string information (AM/PM) too which is can't so it is getting stripped out.
The results is cell one may hold a value like '10:30 AM' but the Format(Now(), "hh:mm AM/PM") code going into the Date variable is resulting in '10:30:00', so the two are never matching as strings. Copy as Matt has suggested and this should work (Dim CurrentTime As String).
Better yet, use the date comparison function DateDiff:-
If DateDiff("s",ActiveSheet.Range("I1"),Time()) > 0 then
SendEmail
End If
This is saying if the time now is greater than the value in I1 (to the second) then run SendEmail.
I don't have the environment to test the solution right now but from what I remember you don't need the brackets in 2007 and also you don't need the format.
Try the following code and see if that fits your need:
If Hour(Now) = ActiveSheet.Range("I1") Then
(...)
End If

Use of System date and user-input date in Windows VS Mac

I have been using this piece of code on my PC running windows and Excel 2013 to return a date.
As shown in the code, the systems prompts for the use of current system date VS use of user-input date.
Function GetRegDate() As Date
Dim RegToday As String
RegToday = MsgBox("Are the transaction(s) of today (System time)?",vbYesNo)
If RegToday = vbYes Then
' Use Current System Date
GetRegDate = Date
Else
' Use user-input date
Dim RegYear, RegMonth, RegDay As Long
RegYear = InputBox("Please enter the year of the transaction.")
RegMonth = InputBox("Please enter the month of the transaction (in number).")
RegDay = InputBox("Please enter the day of the transaction.")
GetRegDate = RegDay & "/" & RegMonth & "/" & RegYear
End If
' Some other codes
End Function
The piece of code has been consistently successful on my PC. But it isn't the case when my friend uses it on his Mac running Excel 2011.
Taking 26 Mar 2016 As an example, on my PC, by using the current system date or the user-input date method, the value "#26/3/2016#" is stored in the variable 'GetRegDate', as reflected from the local variable windows.
But as my friend told me, when using current system date, the value "##" is stored. When using the user-input date method, an error occurs at the line
GetRegDate = RegDay & "/" & RegMonth & "/" & RegYear
with the runtime error 13 type mismatch
I have no idea why this happened. Any help is appreciated!
This is a known bug. From Microsoft Communities:
This is known bug with some of the region settings, working OK when
you use the Dutch region but not for example when you use the French
settings
Got one user that fix it by running this in the Terminal window
defaults write NSGlobalDomain AppleICUDateFormatStrings -dict 1
dd/MM/yy
I hope they fix it soon
That said, you shouldn't implicitly cast to a Date with GetRegDate = RegDay & "/" & RegMonth & "/" & RegYear. Use DateSerial to avoid region issues:
GetRegDate = DateSerial(RegYear, RegMonth, RegDay)

Format a date into mm.dd.yyyy format

I have a VBA script in which I am trying to convert the string in "yyyymmdd" format to "mm/dd/yyyy" format. However, when I incorporate format function to achieve this, it's showing
"Run time error-6": Overflow
Can any one help me with this ? The following is the correspondig VBA code.
// NewDate is in the format "yyyymmdd" being extracted out of a file path like "C:\Files\20140611\file.csv"
Required_format=Format(NewDate,"mm/dd/yyyy") // This line shows the error
you will have to format this yourself, because Format doesn't know that it is dealing with a date, it simply sees a string.
Use something like (psuedo code):
y = left(NewDate, 4)
m = mid(NewDate, 5, 2)
d = right(NewDate, 2)
Required_format = m + "/" + d + "/" + y
Just be absolutely sure the format is consitant. Any change (especially the padding is notorious) messes up your format.
Here is one more solution:
CDate(format(NewDate,"mm dd yyyy"))
Here's another method. As you wrote above, NewDate contains an 8 digit string representing a date in yyyymmdd format. The following "one liner" will output a date (as a string) in your desired format:
Format(Format(NewDate, "####/##/##"), "mm/dd/yyyy")

Getting type mismatch error in replace function in vba

My vba code is to replace current time and date to 4_17_2014 8_09_00 PM format
But i am getting type mismatch error while running below VBA code
Function Current_Date_Time_Stamp()
Dim CurrTime As Date
CurrTime = Now
MsgBox Now
CurrTime = Replace(CurrTime, "-", "_")
MsgBox CurrTime
CurrTime = Replace(CurrTime, ":", "_")
Current_Date_Time_Stamp = CurrTime
End Function
Can anybody help me why i am getting error
As #Tim Williams mentioned in comments, Replace works only with string variables, but CurrTime is date (actually, date variables doesn't store format of date, but only date itself. Format when displaying date depends on regional settings and number format of your cell).
However you can use function with single line of code like this:
Function Current_Date_Time_Stamp()
Current_Date_Time_Stamp = Format(Now, "mm_dd_yyyy h_mm_ss AM/PM;#")
End Function

Resources