VBA interpretation of input date - vba

I have this annoying date format inconsistency with Excel VBA. I have a source text file, which has dates in format of dd/mm/yyyy (i.e. the sane format). But I need to convert it to yyyy/mm/dd (for consistency, I also set up this format as default in my computer).
When I export this text file into database, if both of the dd and mm values are less than 12, VBA treats them as mm/dd/yyyy instead of dd/mm/yyyy. Even I am very confused about this non-standard date format, poor VBA.
For example, I have 06/08/2015 in text file, which is 6th August, but CDate("06/08/2015") returns 2015/06/08, i.e. 8th June. But if the text file has 15/08/2015, VBA can identify it as 2015/08/15.
How to tell VBA that 06/08/2015 is in dd/mm/yyyy format?
Any help with this annoying and tedious task would be greatly appreciated!

Use DateSerial() instead of CDate():
Dim d$
d = "06/08/2015"
MsgBox DateSerial(Right$(d, 4), Mid$(d, 4, 2), Left$(d, 2))
This way you control which bits of the date are which.

Related

Trim function causes date format change

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)

Difference between .Value and .Text of a DateTimePicker

I would like to know the difference between the two.
When I display as VIEW the result is - 12/27/2013 1:48:26 AM.
When I display as TEXT the result is- Friday December 27, 2013
Which is the better choice. I have been told that if date formats are different in pcs (for example one pc has dd,mm,yyyy and the othe pc has mm,dd,yyyy), the Datetimepicker may show error.
DateTimePicker.Value is the DateTime represented by the control
DateTimePicker.Text is HOW the control shows its date to the user according to the Format property
A DateTime value has no inherent format, it is just a numeric representation of a date. The way in which this numeric value is presented to the user is the Format of the date. This format is controlled by the regional settings of the local PC or the server. But it could be changed using various format strings.
DateTimePicker1.Value Will display your Current DateTime Value
DateTimePicker1.Text Will display your Format like Custom , Long, Short
Your default format is Long, that's why is showing DateTimePicker1.Text as Friday December 27, 2013

How to Format VBA TextBox To Long Date

I have a textbox for date display which shows the date in this format: 17/04/2012 but I would like to format it to shows up like 'April 17, 2012'. I tried this code
UserForm1.txtDate = Format(Long Date)
which I am getting syntax error from compiler.Can you please let me know how I can do it?
Thanks
there have been couple of posts on this. Culprit seems to be your system date format. Use of short, medium, long formats will change the result when the system settings change.
In US systems mainly it's done as, e.g. Format$(yourdate, “dd/mm/yyyy”)
Where as European systems, e.g. Format$(yourdate, “short date”)
If we look at explicitly defining your format, you are formulating your regional settings!
Good e.g. short date won’t always be mm/dd/yyyy but could be dd/mm/yyyy. That’s the key advantage over actually specifying the format. Simply you can change how you want your date to look like instead of depending on the system (with or without your knowledge).
When we first encountered this, we had some making the VBA apps and sending it over to another in some other place where they have own localized system date format. Your phone rings... ;)
Short/Long Date: Display a date according to your system’s long date format.
Medium Date: Display a date using the medium date format appropriate for the language version of the host application.
In your case you could use,
UserForm1.txtDate.Value = Format$(Date,"mmmm dd, yyyy")
Or to be super-safe,
Dim dtDate as Date
dtDate = DateSerial(Year(Date), Month(Date), Day(Date))
UserForm1.txtDate.Value = Format$(dtDate, "mmmm dd, yyyy")
Hope that helps.
References

Excel - Copy the displayed value not the actual value

I am a pilot, and use a logbook program called Logten Pro. I have the ability to take excel spreadsheets saved from my work flight management software, and import them into Logten Pro using the CSV format.
My problem however, is that the work flight management software, exports the date and time of take-off of a flight into one cell in the following excel format: DD/MM/YYYY H:MM:SS PM.
This is handled fine by Excel, and is formatted by default to DD/MM/YY even though the actual value is more specific, comprising of the full length date and time group.
This is a problem because Logten Pro will only auto-import the date if it is in DD/MM/YY format, and there is no way to pull out just the displayed DD/MM/YY date rather than the full date time group actual value, unless you manually go through and delete the extra text from the function box.
My question is: Is there a VBA macro that can automatically copy the actual displayed text, and paste it into another cell, changing the actual value as it does, to just the DD/MM/YY value? Additionally, can this be made to work down a whole column rather than individual cells at a time?
Note I have no VBA experience so the perfect answer would just be a complete VBA string I could copy and paste.
Thank You.
As pointed out in the comments, you'd better not use VBA but formulas instead.
This formula:
TEXT(A1,"dd-mm-yyy")
will return the formated date in a text way. You can drag and drop the formula in the whole range of your cells and Copy/Paste Special > Values so that you will only have the needed values to get imported in Logten Pro.
There are three options using formulas.
Rounddown
Excel stores the date time as a number and uses formatting to display it as a date.
The format is date.time, where the integer is the date and the fraction is the time.
As an example
01/01/2012 10:30:00 PM is stored as 40909.9375
All the values after the decimal place relate to the hours and minutes
So a formula could be used to round the number down to a whole number.
=ROUNDDOWN(A1,0)
Then format the value as a short date.
It will then display as 01/01/2012
INT
As above, but using a different formula to get rid of the fraction (time)
=INT(A1)
Text
Alternately the date only could be extracted as text using this formula
=TEXT(A1,"dd/mm/yyyy")
It will then display as 01/01/2012
I'm a bit late to the party on this one, but recently came across this as was searching for answers to a similar problem.
Here is the answer I finally came up with.
Option Explicit
Sub ValuesToDisplayValues()
Dim ThisRange As Range, ThisCell As Range
Set ThisRange = Selection
For Each ThisCell In ThisRange
ThisCell.Value = WorksheetFunction.Text(ThisCell.Value, ThisCell.NumberFormat)
Next ThisCell
End Sub
This answers the question as asked, apart from the new values are pasted over the existing ones, not into a new cell, as there is no simple way to know where you would want the new values to be pasted. It will work on the whole range of selected cells, so you can do a whole column if needed.

How to format the date format as system date format in Excel VBA

It is require to change the date column in excel as system date format.
if the system date format will be mm/dd/yyyy
In excel sheet i need to change it as mm/dd/yyyy
How to change it through VBA Excel?
Thanks in advance.
You can change the date format of cell to the regional (Windows) settings this way:
Select the cells you want to change
Right-click > Format Cells...
Tab "Number", choose "Date"
Choose a date begining with an asterisk *: *05/13/2011
More information on this SO thread: What is the best way to say format is mm/dd/yyyy - but in the local format?
[EDIT] With VBA you can do
Sub test()
Selection.NumberFormat = "m/d/yyyy"
End Sub
m/d/yyyy represents the regional settings as described above