How to format the date format as system date format in Excel VBA - 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

Related

How to convert dates to valid format in Google Data Studio?

I have a google sheet with dates in europian format ( 15.09.2020 08:40 ) . When importing the sheet to data studio it detects this field as text. I try to change it to DATE but get error "Cant convert". Maybe becouse its in european format? Anyone know how to change this? I have tried formatting in google sheets with no luck...
You have to convert it yourself, with the european date as as string in the field your_field, create an additional field with following formula:
PARSE_DATETIME("%d.%m.%Y %H:%M:%S", your_field)
The details for date converting/parsing can be found here:
https://support.google.com/datastudio/answer/9739558

excel: change format dates in advanced filter

I'm using the Advanced Filter to search between dates in a database.
I placed my criteria side by side so I can seach for one or more information together.
Here is the example:
This is the code:
Sub FiltroAutomatico()
'
' FiltroAutomatico Macro
'
'
Sheets("BASE_TOTAL_ATUAL").Range("A1:J445").AdvancedFilter Action:= _
xlFilterCopy, CriteriaRange:=Range("CONSULTA!Criteria"),
CopyToRange:=Range _
("CONSULTA!Extract"), Unique:=False
Range("H25").Select
ActiveWindow.SmallScroll Down:=-15
Range("G3").Select
End Sub
Anyway, the problem is that when I search for something like >03/03/2017 and <25/03/2017 it didn't work as I expected.
I think the problem it's because Excel it's not recognizing my dates correctly (my Excel is in portuguese and the date here has the dd/mm/yyyy format). I tested using US dates instead and it worked perfectly.
Is there any way to change the format dates from an Advanced Filter to understand that the format of my criteria is dd/mm/yyyy instead of mm/dd/yyyy?
I cannot test this, but dates in VBA filters have always been US-centric. I would suggest that you have your users enter the actual starting and end date in your local format (but NOT in the actual criteria range). In the criteria range itself, use this formula:
=">" & StartDate
="<" & EndDate
Where StartDate and EndDate refer to the dates entered by your user.
You could even leave it in the same physical location as you show above, but have the criteria range elsewhere. Or you could have them enter it on a User Form.
The cell will display something like >49076 where the number is the number of days since (usually) 1/1/1900. But it should work.
If you have them enter the dates elsewhere, be sure to LOCK and PROTECT the criteria cells so the user cannot delete or change the formula.

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)

Crystal reports change date format

I am working on Crystal report 2013 and I'd like to format the date in crystal report
from
2008-04-27
to
April 27, 2008
what i have tried is this
Right click on the field -> Format Editor
Date and Time tab
Select date/time formatting you desire
but i can't see the date and time tab when i did this. I also use this formula below but no luck.
CSTR({StudentInformation.BirthDay}, "MM dd, yyyy")
Can anyone please help me to solve this. thanks
create a formula for your date and in your formula set it to totext( {StudentInformation.BirthDay}, "MMMM dd, yyyy" ), then add the formula to your report instead of the field
After having my research. This code solve my problem. It converts the string data type to Date and you can now easily format this according to your need.
DateValue({StudentInformation.BirthDay})

VBA interpretation of input date

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.