Why does VBA interpret "093 0005" as a date?
In MS Access VBA, the following line:
Format("093 0005", "0000000000000") returns "0000000034090"
34090 is the vba numeric equivalent of the date 5/1/1993.
In most cases this kind of date assumption does not occur. For example:
Format("092 0250", "0000000000000") returns "092 0250", i.e. does not try to apply formatting.
Format("0930005", "0000000000000") returns "0000000930005" as expected.
The only solution to this that I have come up with so far is to code around using this function when the inbound string contains a space or use the IsDate function.
This has nothing to do with Access per se. In any VBA, the format function will behave like this if the text is either separated by a SPACE, HYPHEN, or a DASH. So this
Debug.Print Format("093 0005", "0000000000000")
Debug.Print Format("093/0005", "0000000000000")
Debug.Print Format("093-0005", "0000000000000")
will return 0000000034090
It will try and convert it to date or a number and if it is a valid date or number then it will show you the numeric equivalent of it. And if it is not a date or number equivalent then it will leave it as it is. I believe the reason for this is that the Format function is unable to ascertain the "Format" of the value and takes it as a string. It is similar to then saying Format("Blah", "0000000000000"). It is but obvious that you will not expect Format function to format it as 000000000blah.
Unfortunately I couldn't find any article in MS knowledge-base which explains why Format function behaves like this.
The only way that I have discovered in the past to get around it is to use the VAL function to convert it to a number. So
Debug.Print Format(Val("093 0005"), "0000000000000")
will give you the desired result 0000000930005
However if the numbers are separated by a HYPHEN or a DASH then the VAL function is useless. For that you will have to use REPLACE to replace the HYPHEN or a DASH with SPACE. However I doubt that you will be formatting numbers which have a HYPHEN or a DASH.
Related
Firstly, important to note that I'm in the UK so standard date format is dd/mm/yyyy
In a A1, I have a date: 02/05/2017 (dd/mm/yyyy)
I can confirm this in the immediate window:
?CLng(Range("A1").Value)
42857
Now, if I do the following:
Range("A1").Value = Range("A1").Value
you can probably guess, nothing happens - the date is still 02/05/2017 and the numeric value is still 42857
But if I use trim with it:
Range("A1").Value = Trim(Range("A1").Value)
The date is changed to 05/02/2017. This isn't just formatting - the numeric value has also changed to 42771.
What is it about the Trim() method that causes the date to be read in US format and then converted back to UK format with a new date value? Is this a bug?
From the discussion in comments:
The default or "token" format in VBA (not Excel itself, as Macro Man rightly pointed out) is US English - regardless of regional settings or cell formatting.
When you do VBA text functions on a date, the output of those functions are in text format. So the result of Trim(Range("A1").Value) is a string. This string happens to resemble a proper US date, so when you insert it into a cell, Excel recognizes it as a US date.
So two implicit conversions happen. The first happens when you read the cell contents and pass it to trim(): date->text conversion; the second happens when you write it back to an Excel cell: text->date conversion. The second conversion has no information about the format, so it assumes US English.
(You should be able to achieve the same result with any text function, not just trim().)
I found out that if you add "'" before the date, the date is not altered.
Range("A1").Value = "'"&Trim(Range("A1").Value)
To summarize my problem, I'm currently having the exact same issue as the person from this question here. I'm trying to parse a US date into an Excel sheet that has German as default and gives me a type mismatch on most of the dates because of it.
SetLocale sounded like the perfect solution to my issue, but after a minute of further research I discovered that GetLocale and SetLocale are apparently not supported in VBA.
It sort of worked when I assigned the parsed date to a String variable (I end up with a column using either MM/DD/YYYY or DD/MMM/YYYY format depending on whether or not I had a type mismatch, as long as I use On Error Resume), but I need them in a MM/DD/YYYY date type/format in order to compare all the parsed dates to a specific date in another cell (attempting to determine if the site has had any updates since the date entered in the specific cell).
I've also tried doing TimeStamp = Format(TimeStamp, "MM/DD/YYYY") (TimeStamp being a variable containing the parsed date), but it doesn't seem to be working- most likely due to the type mismatch error.
If anyone knows a VBA equivalent of the SetLocale function used in the linked question for me to try out, I would greatly appreciate it. If there isn't any, I'll be happy to amend my question and add my current code here to try and hammer out a solution together.
Thank you for your time and your help.
If you know they are all in US format, you can use a function like this:
Function ConvertUSDate(sDate) As Date
ConvertUSDate = Evaluate("DATEVALUE(""" & sDate & """)")
End Function
I wonder if there's any function in VBA which convert strings into a date?
I mean I work with dates with different formats e.g. 20150723, 07023015, 23-07-15 etc. -it's the same date for me, but VBA editor does not know it:)
What's the best way to get "real" date 2015/07/23 from string 20150723 ?
For your specific example, you can use:
cdate(format("20150723", "0000-00-00"))
which converts "20150723" into "2015-07-23" which should be recognisable to CDate as a date string.
In VB.net, I want to make a counting program using a Function and a Sub.
There is a textbox to input a date and a button to exercise the programme in Form1.
I have a txt file which was extracted from MS-Excel with sequential date of time at its column A.
And from that txt file, I want to count the number of date(Actually string) such as "18-Jun-12".
The answer showing the count should be in the format of msgbox in the Sub.
I really have no idea how to link a Function and a Sub using variable, because I am just beginner.
Any help will be gratefully accepted.
If the fields are delimited by comma you must be careful since the field itself could contain a comma. Then you cannot differentiate between the value and the delimiter. You either could enclose the fields with quotes to mask them. But then you should use an available CSV parser anyway.
If the values never contain comma and you want a simple solution use File.ReadLines or File.ReadAllLines to read the lines and String.Split to get all fields per line.
Here's a simple approach using a little bit of LINQ to count all lines which contain the searched date (as string):
Dim linesWithThatDate = From line in File.ReadLines("Path to File")
Where line.Split(","c)(0).Trim() = "18-Jun-12"
Dim count = linesWithThatDate.Count()
As an aside, if the user must enter a date you could use a DateTimePicker control instead. Then you should also use Date.Parse(line.Split(","c)(0).Trim()) or Date.TryParse to get a real date.
I'm trying to format some cells in a Reporting Services report that will contain DateTime? values - or not.
If the underlying data has a NULL for that DateTime?, I'd like to show nothing (empty cell) - and if that data source contains a value, I'd like to show the date in short date format (dd.MM.yyyy in my locale).
So I tried to put this formula into the relevant SSRS cells
=FormatDateTime(Fields!DatumBSE.Value, 2)
but now I'm getting 01.01.0001 for all NULL dates....
I can't seem to wrap my head around how to do this in a SSRS (VB) formula.... I tried using IsNothing() but that doesn't seems to really help - I can detect a NULL, but how to I tell the cell to show an empty string in that case?
Solution:
I ended up using this function:
=IIF(IsNothing(Fields!DatumBSE.Value), "", FormatDateTime(Fields!DatumBSE.Value, 2))
Seems to work just fine for me now.
I just tested the following expression and it replaced the null date with an empty string:
=IIF(Fields!DatumBSE.Value is nothing, nothing, FormatDateTime(Fields!DatumBSE.Value, 2))
The other suggestion that I would make is that you could format the date to the correct format in the report dataset by placing a CASE expression around the date value.
use a code like this:
If(isNull([date field]),Null, elsequote)