I was wondering if there is any possibility to write a VBA code where the column A should always have a date format like this: 12.10.2017 (not 12/10/2017 or 12-10-2017). If anything else is written in the column A like "12" or "car" the entry should be deleted. It has to accept only the date format mentioned above.
I used data validation for this, with length 10 and the date format to take only "." into consideration, but I want to do it as a VBA code instead.
Thanks!
A valid date is a long representing the number of days since the 1st january 1900. So a valid date would be 45603. You can display this date in any format you wish using the format codes d, m and y . So to display the date as dd.mm.yyyy then set that numberformat in the cells in column A. Your problem though is that Excel will only accept a date entered as either a long or in a built in date format (using /, - or space as a separator). You could allow the users to enter a text string in the format dd.mm.yyyy and then convert that string into a date and then reject it if the conversion didn't result in a valid date - but wouldn't it be easier to just train your users to enter dates correctly?
Related
I have an .xlsx file where cells of column C have format Date (short). In the cells is visible a date in this format: 30.1.2017.
If you switch the cells format to General you will see numbers like this: 42765.
I need to insert new line, set format Date (short) on C cell of new column and insert the date that I have in string. Cannot find approriate examples. How could it be done?
MS Excel stores dates as a serialized timestamp value, a count of the number of days since 1st January 1900 (or 1st January 1904 if created on a Mac)... that's what your 42765 value is.
PHPExcel provides a number of functions for date handling, which can be found in the PHPExcel_Shared_Date class.
You can convert a unix timestamp or a PHP DateTime object to an MS Excel serialized timestamp using the PHPToExcel() method. If you have a string value for your date, then convert it to a PHP DateTime object first.
Then you can set the cell value to the resulting timestamp value, and apply a number format mask to tell Excel how that date should be displayed. The 02types.php example in the Examples folder demonstrates this:
$dateTimeNow = time();
$objPHPExcel->getActiveSheet()
->setCellValue('C9', PHPExcel_Shared_Date::PHPToExcel( $dateTimeNow ));
$objPHPExcel->getActiveSheet()
->getStyle('C9')
->getNumberFormat()
->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD2);
And it is also explained in the PHPExcel Documentation
I have a date in format of YYYY-MM-DDThh:mm:ss
Please provide a GREL expression that increments date to 1 month from the present date value for all cells in the column in openrefine. Thanks!
First you need to make sure the data in the cells is of type 'date' - if the text in the cell is in green then the data is already 'date' type. Otherwise you will need to convert it using the GREL:
value.toDate()
Screenshot - before converting to date
Screenshot - after converting to date
Once you have the data as Date type, then you can use the following GREL to increment by one month:
value.inc(1,'month')
For more on using dates in OpenRefine see https://github.com/OpenRefine/OpenRefine/wiki/GREL-Date-Functions
My question is very similar to this problem, but for Excel: how do I convert mmyy to last day of month in netezza or this one: Create a date from Credit Card expire in MMYY format.
Essentially I have a column filled with dates that have been written as mmyy i.e.
0215
0215
0215
0315
0315
The column has been saved as the data type: "Special".
How do I convert this data into a useable format? I don't mind if we put it into dd/mm/yyyy and use the first or the last day of the month that's fine. Is there an Excel function that I could use or is this something I would have to do in VBA and if so, how?
With the 'dates' starting in A2, use this formula in an unused column to the right,
=DATE(20&RIGHT(A2, 2), LEFT(TEXT(A2, "0000"), 2), 1)
Fill down as necessary. You will have to format as a date or you will receive the raw date value like 42064.
If you need the end-of-month, add 1 to the month and change the day to 0 like,
=DATE(20&RIGHT(A2, 2), LEFT(TEXT(A2, "0000"), 2)+1, 0)
30 characters:
=1*REPLACE("1/"&A1,5,1,"/201")
Subject to the dates concerned the 20 may be redundant.
use this
=DATEVALUE("01/"&LEFT(A1;2)&"/20"&RIGHT(A1;2))
do not forget to change Format Cells to "Date" for column "B"
I'm having a hard time in converting the date from dd-mm-yyyy format to dd/mm/yyyy format.
For Example,
When i enter a date in excel as 25/02/2012 (dd/mm/yyyy), after entering the date if go in the next line it converts the date in the 25-02-2012 (dd-mm-yyyy) format.
what i want to do is that when i enter the date in (dd/mm/yyyy) format in excel it should keep it as it is and should not change it back to (dd-mm-yyyy) format when i go the next cell.
when i enter my date as the current system date my code gives me an error, i am having trouble validating the date i.e. is the date entered is a valid date or not
Sub valid_date()
' ---------------------------------------------------------------------
' Final Code - Trial
' ---------------------------------------------------------------------
Dim d1 As Variant
Dim IssueDate As Variant
Dim str As Variant
d1 = Worksheets("Sheet1").Cells(6, 1).value
MsgBox " The Issue Date format is " & d1
sysdate = Date
MsgBox "System Date is " & sysdate
If IsDateValid(d1) Then ' if date is in dd/mm/yyyy format then print this
If (d1 > sysdate) Then
MsgBox "Invalid date"
End If
End If
End Sub
Function IsDateValid(pdate) As Boolean
IsDateValid = False
Set RegExp = CreateObject("VBScript.RegExp")
' it only matches whether date is in dd/mm/yyyy format or not
'
' [1-9]|0[1-9]|1[0-9]|2[0-9]|3[0-1] ---> it allows the DATE from 01 to 31
' [1-9]|0[1-9]|1[0-2] ---> it allows the MONTH from 01 to 12
' 1[9][0-9][0-9]|2[0][0-9][0-9] ---> it allows the YEAR from 1900 to 2099
'
' below is the regular expression for checking the date in dd/mm/yyyy format
RegExp.Pattern = "^([1-9]|0[1-9]|1[0-9]|2[0-9]|3[0-1])[/]([1-9]|0[1-9]|1[0-2])[/](1[9][0-9][0-9]|2[0][0-9][0-9])$"
' check whether the date is in dd/mm/yyyy format or not....
tempdate = RegExp.Test(pdate)
If tempdate Then ' if tempdate is in dd/mm/yyyy format than proceed further
'If isdate(tempdate) Then ' if date is a valid date then proceed further
If isdate(pdate) Then
IsDateValid = True
Else
IsDateValid = False
End If
Else
IsDateValid = False
End If
End Function
i'm using the above mentioned code by using a regular expression to check whether the date is in dd/mm/yyyy format or not
but the problem which i'm facing is that it takes the date in excel as dd-mm-yyyy format whenever i enter the date in dd/mm/yyyy format.
i have updated my code a bit,
i also need one more help when i enter my date as the current system date it gives me error
for example,
when i enter my date as 09/09/2012 (suppose this your current system date) and when i check this date using IsDate method, it gives me an error
i have again edited my code,
Can anyone please help me on this
You don't need VBA/RegEx. Select the cells/columns where you input dates and create a Custom number format: dd/mm/yyyy. Now no matter how you type in a valid date (05-05-2000, 3-1-2010, 14/6-1990, etc.), it should be formatted as dd/mm/yyyy.
And, as Olle points out, you should use the Date object rather than Variant if you are going to be manipulating dates in VBA. This way you're working with the serial number and not a string with potential formatting issues.
First, I suggest you check the regional settings for dates on your computer. If you set it to use the "dd/mm/yyyy" format it will be used by Excel as well and hopefully remove the need for any RegEx VBA-code.
Second, if you do need to use VBA to reformat dates, I strongly suggest you use the Date data type instead of Variants. I also advise you to use Option Explicit at the top of your code and explicitly declare any variables in order to minimize typos and produce better quality code.
Third, I've looked through your code some more and it seems it will never work:
1. Because it is never declared, tempdate is a Variant
2. You assign tempdate to be a boolean, from the result of RegExp.Test(pdate)
3. So when you check IsDate(tempdate) it will always be false, since a boolean can never be a Date.
Again, if you use the Date data type, you can skip the RegEx... :)
I use Adobe online PDF to Excel and dates display correctly as MM/DD/YYYY but when extracting month (=Month()) it returns the DD portion. It is being interpreted as DD/MM/YYYY. I saved the file as a .csv closed and restarted excel and opened the .csv file and the dates were correct MM/DD/YYYY.
I want to create a function, which has a paramater ( a string which contains a date ) and then the function converts it and returns it. In our company we have workstations with three different languages. We have hungarian, english and german workstations too. I want to read a date from the registry, but this date will be written into the registry according to the current regional setting.
So if the regional setting is hungarian, then date written to date registry is 2012.01.25 (YYY.MM.DD), but if i change the regional setting to german then the value written to the registry will be 25/01/2012 (MM.DD.YYYY). If i change the regional setting to english, then the value will be 01/25/2012 (DD.MM.YYYY).
Unfortunately i don't know which regional setting was used when the date was written into the registry, because it can be changed since the value was written into the registry.
This iy why i want to create a function which gets a date, and then converts it to this format: YYYY.MM.DD. but i don't know how to do it.
Could someone help me how to do this?
Thanks!
Are you managing this registry value directly or it belongs to another software and just trying tomread it?
If it's yours, then
A. if it is a string value, then simply format it before storing, to ISO format (yyyy-MM-dd), format or formatdate function will do the trick
B. if it is a binary value, convert the datetime value to double with cdbl and store that
Well, if it's not yours, then it's not your lucky day. I've done it a couple of years ago and I used the text around the date to make an assumption on the format ...
You can use this expression to convert your strings to SQL type date. This expression uses the DD/MM/YYYY format only when it cannot use its default MM/DD/YYYY format.
CASE
WHEN CHARINDEX('/', val)=5 THEN CONVERT(date,val,111)
ELSE CONVERT(date, val, CASE WHEN LEFT(val,2) <= '12' THEN 101 ELSE 103 END)
END
This expression should be used inside a select statement. I am assuming that the name of your varchar column containing a date string is val.
If you are saving those dates on a date type of column (DATETIME, DATE, SMALLDATETIME, etc), then it won't matter how it is displayed or under what language it was saved. If you want to convert a DATE to a VARCHAR on the format YYYY.MM.DD then you can use: CONVERT(VARCHAR(10),YourDate,102).