We have an internal software that exports data reports to Excel, and several of the columns contain dates in the MM/DD/YYYY format. I have a spreadsheet setup where I will copy these exported reports over to, and then I have formulas already setup to look for these dates. The problem currently is that all dates in months 1-9 come in like this: 01/22/2017.
The formulas do not recognize them until I activate each and every cell, and hit enter, and then it re-formats to: 1/22/2017 and then everything works. Currently I am having to go cell by cell and activate, and hit enter. Simply selecting all of the cells, and changing the formatting to a Date hasn't worked. Is there a faster way around this? I'm open to VBA if it works, however I'm wondering if there is a simpler method I'm missing, or simply a way to get my formulas to recognize the original date formatting.
EDIT:
The data is exported from a SQL database. The formulas that need to reference are using <= in reference to week start dates on another sheet. I initially tried having those match this formatting, but because the dates come as General, math operators don't work at that point.
Select the date column and then click Data > Text to Columns
Next, Next, then select Date 'MDY'
then you should be able to do this using a number format of m/dd/yyyy or m/d/yyyy if you don't want a leading zero in the days as well
select the range and run:
selection.value = selection.value
(for some cases this does not work)... in such moments this should do:
selection.value = evaluate("INDEX(" & selection.address & "+0,)")
just keep in mind that there may be a need of changing the formatting of the cell...
This also can be done in a formula, without having to go through menu options, thus making it more automatic. Use TEXT and command and format the date exactly how you want. Leaving off leading zeros on the month or day is as simple as including just one character for them instead of two:
=TEXT(date-field,"M/D/YYYY")
instead of
=TEXT(date-field,"MM/DD/YYYY")
Related
I'm using closedxml to generate a excel file.
I'm mapping the cells directly in excel by creating a named range and inside of it reaching the items in the range by {{item.property}}, the range collection is added like a variable in the code.
The item I'm using has a Year property and a month property, the mapping in that excel cell is: {{item.Yr}}/{{item.Mon}}.
I would like Excel to format the value in the cell to mmm-yyyy (Jan-2020). Which it does if I directly write a date in the cell in Excel and presses Enter, but that's not what I want, I want it to format my generated value.
I've tried using the functions like =TEXT(date,format) or =DATEVALUE("1/1/2015").
But the problem is that I can't write directly "1/1/2015", I need to write something like DATEVALUE(1/{{item.Mon}}/{{item.Yr}} but Excel syntax doesn't allow this. Ofcourse I've also tried just the "format cell" to date on the cell, but doesn't work (only if I directly type a date and presses Enter)
I'm sorry for my crappy explanation. I hope someone understands what I mean and what I want to accomplish.
SOLVED
After 2 days banging my head against the wall, I found that If a variable is used in a formula, it needs to be escaped with "&" at the beginning:
&=Date({{item.Yr}},{{item.Mon}},1).
My data is extracted from an application and it has a text that looks like a date/time in excel. How do I actually convert "3/24/2016 11:22:07 PM" (in text) to a real date/time conversion? I've tried formatting the cells but it doesn't work.
For a date conversion:
=DATEVALUE(TEXT(A1,"MM/DD/YYYY"))
For a time conversion:
=TIMEVALUE(TEXT(A1,"HH:MM:SS"))
For datetime conversion:
=DATEVALUE(TEXT(A1,"MM/DD/YYYY"))+TIMEVALUE(TEXT(A1,"HH:MM:SS"))
Where A1 has the data you wish to convert.
By the way, then you may wish to format the cell to a date/time or whatever.
Hope that helps.
1) try using the DATEVALUE function and see if that works for you.
2) A more reliable way, since datevalue does not always work is to strip the text out manually and insert it into and excel date value. You are going to want to use a combination of the following functions:
DATE
TIME
IF
FIND
MID
LEFT
RIGHT
LEN
Now in my opinion the easiest way to do this is to work with multiple helper columns to build out all the steps. One column per step. When you get your final answer, you can substitute or copy paste your formulas from the helper columns into the final formula until you are left with one variable. The reason I say this is that the final formula referring to only 1 variable gets rather lengthy/ugly and very hard to trouble shoot if you make a typo, forget a bracket or something goes wrong. When I did this approach I used a totally of 14 columns (includes final formula). When I packed it all up into 1 formula it resulted in this:
DATE(RIGHT(LEFT(A3,FIND(" ",A3)-1),4),LEFT(LEFT(A3,FIND(" ",A3)-1),FIND("/",LEFT(A3,FIND(" ",A3)-1))-1),MID(LEFT(A3,FIND(" ",A3)-1),FIND("/",LEFT(A3,FIND(" ",A3)-1))+1,FIND("/",LEFT(A3,FIND(" ",A3)-1),FIND("/",LEFT(A3,FIND(" ",A3)-1))+1)-FIND("/",LEFT(A3,FIND(" ",A3)-1))-1))+TIME(LEFT(RIGHT(A3,LEN(A3)-FIND(" ",A3)),FIND(":",RIGHT(A3,LEN(A3)-FIND(" ",A3)))-1)+IF(AND(LEFT(RIGHT(A3,LEN(A3)-FIND(" ",A3)),FIND(":",RIGHT(A3,LEN(A3)-FIND(" ",A3)))-1)<12,RIGHT(RIGHT(A3,LEN(A3)-FIND(" ",A3)),2)="AM"),0,12),MID(RIGHT(A3,LEN(A3)-FIND(" ",A3)),FIND(":",RIGHT(A3,LEN(A3)-FIND(" ",A3)))+1,FIND(":",RIGHT(A3,LEN(A3)-FIND(" ",A3)),FIND(":",RIGHT(A3,LEN(A3)-FIND(" ",A3)))+1)-FIND(":",RIGHT(A3,LEN(A3)-FIND(" ",A3)))-1),MID(RIGHT(A3,LEN(A3)-FIND(" ",A3)),FIND(":",RIGHT(A3,LEN(A3)-FIND(" ",A3)),FIND(":",RIGHT(A3,LEN(A3)-FIND(" ",A3)))+1)+1,2))
Note it is set up using cell A3 as the one with the time as text that needs formatting.
3) You should also be able to use excel's text to column function located on the DATA ribbon about half way across.
4) And of course there will be a way to code it through VBA as an option as well.
=DATEVALUE(A1)+TIMEVALUE(A1) seems to work as well, since each function only returns the value corresponding to what it recognizes in the string. That is, DATEVALUE() ignores the time component, while TIMEVALUE() ignores the date component.
Using VBA, I import a csv file and put a bunch of data into several columns.
One of these columns has a date and time. As I need to be able to use just the 'time' part of these cells, I try to convert the entire column to Time by using (and just about every other variation)
Cells(x.y).EntireColumn.NumberFormat = "hh:mm:ss"
or
Range("C1").NumberFormat = "hh:mm:ss"
Range("C1").EntireColumn.NumberFormat = "hh:mm:ss"
However, this does not convert the entire column. I've tried every possible other way of selecting the entire column and changing it (through VB) however still only a portion remains converted.
If I doubleclick on these unconverted cells and press enter they change to the correct format. I realise this is a common problem relating to Calculations but my workbook is set to Automatic Calculations and I've tried setting this in VB too. This doesn't change anything.
The only pattern I can find is that the cells stop being converted when the Day reaches double digits. For example:
Column C
01/05/2013 7:28:56
03/05/2013 13:24:53
07/05/2013 20:13:24
09/05/2013 8:29:22
12/05/2013 9:28:56
15/05/2013 21:14:25
17/05/2013 7:28:56
Becomes:
Column C
7:28:56
13:24:53
20:13:24
8:29:22
12/05/2013 9:28:56
15/05/2013 21:14:25
17/05/2013 7:28:56
In the formula bar up the top for each cell it still shows the whole Date and Time for all cells, not sure if this is related, but doesn't seem to matter in terms of the calculations i have to perform using the Time.
Essentially I have to take the time for a cell in column C and the time from another Cell (also in Date/Time format) and check the difference. After some research I decided the best way was to convert all the cells to a time format and then do my calculations.
Alternatively I could try converting the column to text and using a Split function (using space as a delimiter) and pulling the time out, but I'm having trouble doing this too, as once again trying to convert the entire column to text stops at the double digits for date.
Thanks for reading through all that, any thoughts and help would be appreciated.
Edit: Realised some of my syntax was incorrect in my post, this was however correct inside my macro
another edit: I think this definitely has something to do with the date format... I just realised that before i format them, the dates are m/dd/yyyy and then when it gets to actual double digit days it changes to dd/mm/yyyy, and thats when the problem occurs...
In order to avoid confusion, and as the date always seems to occupy the same width, I recommend to
1) import this column as a text
2) then go over the whole column
For Each C In Range("A:A").Cells
If C <> "" Then
' ....
End If
Next C
3) cut away the leading 11 positions, e.g. C = Mid(C, 11, 99)
4) convert the remaining string to a time, e.g. C = CDate(C) (... yes it works with a time as well, because a time is a fractional part of a date)
Alternatively you may want to capture the date part and bring it into shape, too. See here for some ideas using worksheet formulas.
I copy a sheet from html page.Because I need the sheet structure,the "paste as text" selection is no use here.
The data will be like this
2/3 3/4
1012/2332 332/665
when paste to excel
2/3/2012 3/4/2012
1012/2332 332/665
the data cannot be parsed to date form is safe while those which can will be changed and cannot be changed back in format cell.
How to disable this feature of excel,really annoying.
This is an obnoxious problem. Have you tried setting the cell format to Text before pasting as text? this works for me in 2003. I'm afraid simply doing one or the other is not enough, and the order is important.
If this works (and you need to do this frequently or regularly), you can set up a custom macro (e.g. CTRL-SHIFT-P, etc.) to set the format and do the paste all in one motion.
EDIT:
For the record, here is what the different paste methods produced for "2/3":
Paste (normal) 3-Feb
TXT Format->Paste 3-Feb
Paste->TXT Format 40942
Paste UNI 3-Feb
*TXT Format->Paste UNI 2/3
Paste UNI->TXT Format 40942
Paste TXT 3-Feb
*TXT Format->Paste TXT 2/3
Paste TXT->TXT Formar 40942
Up until recently (2/27/2013) the Excel data import of the bank account text file information handled the date field of the text file correctly. It isn't clear to me why this changed, but it lead to my looking here for a solution.
I'd like to propose to you a different solution than above...
copy the offending dates to a separate excel sheet
use the Mid function ( mid(string, start, number of chars) to extract each date component to
a separate column in the same row (Look at columns B, C, and D
in the next column use the Date function ( date(yr, month, day) to convert the date components
from the prior three columns into something excel will deign to recognize as a date (remember,
computers are basically stupid, and Microsoft programs are about the contrariest things on the
planet!)
PASTE SPECIAL the values from your date column back to your worksheet
VOILA! You have fixed a problem that you should NEVER have had in the first place.
I tried to paste the spreadsheet here (and also by hand typing, but this display collapses repeating characters. Suffice to say it looks like this...
02/27/2013 02 27 2013 02/27/2013
original mm dd yyyy value ms recognizes as a date which you can paste by value back to spreadsheet
the formulas for those not familiar with Excel...
un-date from worksheet in column at (excel sees as a string...not a number!)
for month: mid(a1,1,2) (take the thing in column a1, start at the 1st character, and grab two chars)
for day: mid(a1,4, 2) (take the same thing from column a1, grab the two chars starting at the 4th)
for year: mid(a1,7,4) ...
making it a date: you now have month in column b1, day in column c1, and year in column d1, so
in column e1 you say: date(d1, b1, c1), and then you see the same date as shown in column a1, however, this will now be recognized by Excel as a date! Copy this and PASTE SPECIAL>Value to the column in your original worksheet and it will respond.
I had looked in the original column for a ' character which denotes treat this value as text only, but I hadn't found one. I do not know why Excel has become pesky like this, but I have come to expect this type of frustrating and annoying behavior from Microsoft programs. Yes, I do still buy the darn things, but often choose open source code when I can because of these "features".
Good luck.
I just came across this problem and found a really good solution. Here are my steps:
Copy and paste so that you can work out the area to be selected.
Delete the contents of the cells
Change the formatting to TEXT
re-paste but use the paste button in the menu bar and choose 'Match Destination formatting'.
This works like a dream! :)
Here is a list of dates:
04-22-11
12-19-11
11-04-11
12-08-11
09-27-11
09-27-11
04-01-11
When you copy this list in Excel, some of them are recognized as dates, others not, in the following manner:
04-22-11
12-19-11
11-04-11 (date)
12-08-11 (date)
09-27-11
09-27-11
04-01-11 (date)
Does anyone know why? And how to force Excel to recognize all list items as dates?
Many thanks!
It is caused by Excel auto-recognizing/formatting the cell contents, but in unclear/inconsistent ways.
Fixing it is not that hard...
Check out this forum post:
http://www.pcreview.co.uk/forums/excel-not-recognizing-dates-dates-t3139469.html
The steps in short:
Select only the column of "dates"
Click Data > Text to Columns
Click Next
Click Next
In step 3 of the wizard, check "Date" under Col data format, then
choose: "DMY" from the droplist.
Click Finish
This is caused by the regional settings of your computer.
When you paste data into excel it is only a bunch of strings (not dates).
Excel has some logic in it to recognize your current data formats as well as a few similar date formats or obvious date formats where it can assume it is a date. When it is able to match your pasted in data to a valid date then it will format it as a date in the cell it is in.
Your specific example is due to your list of dates is formatted as "m/d/yy" which is US format. it pastes correctly in my excel because I have my regional setting set to "US English" (even though I'm Canadian :) )
If you system is set to Canadian English/French format then it will expect "d/m/yy" format and not recognize any date where the month is > 13.
The best way to import data, that contains dates, into excel is to copy it in this format.
2011-04-22
2011-12-19
2011-11-04
2011-12-08
2011-09-27
2011-09-27
2011-04-01
Which is "yyyy-MM-dd", this format is recognized the same way on every computer I have ever seen (is often refered to as ODBC format or Standard format) where the units are always from greatest to least weight ("yyyy-MM-dd HH:mm:ss.fff") another side effect is it will sort correctly as a string.
To avoid swaping your regional settings back and forth you may consider writting a macro in excel to paste the data in. a simple popup format and some basic logic to reformat the dates would not be too difficult.
In your case it is probably taking them in DD-MM-YY format, not MM-DD-YY.
The quickest and easiest way to fix this is to do a find and replace on your date seperator, with the same separator.
For example in this case Find "-" and Replace with "-", not sure why this works but you will find all dates are right-aligned as they should be after doing this.
Here is what worked for me. I highlighted the column with all my dates. Under the Data tab, I selected 'text to columns' and selected the 'Delimited' box, I hit next and finish. Although it didn't seem like anything changed, Excel now read the column as dates and I was able to sort by dates.
The simplest solution is to put yy,mm,dd into the date() formula by first extracting them with left(), mid() and right(). In this case, assuming your input date is in A1:
=date(right(A1,2)+100,left(A1,2),mid(A1,4,2))
Explanation of above:
=right(A1,2) gets the last two digits in the cell (yy). We add 100 because it defaults to 1911 instead 2011 (omit +100 if it doesn't do that on yours)
=left(A1,2) gets the first two digits in the cell (mm).
=mid(A1,4,2) gets 2 digits in the middle of the cell, starting at 4th digit (dd).
Why this happens in the first place:
I come across this problem all the time when I import Canadian bank data into excel. In short, your input date format does not match your regional settings.
Seems your setting mean Excel wants date input as either DD-MM-YY or YY-MM-DD, but your input data is formatted as MM-DD-YY.
So, excel sees your days as months and vice-versa, which means any date with day below 12 will be recognized as a date, BUT THE WRONG DATE (month and day reversed) and any date with day above 12 won't be recognized as a date at all, because Excel sees the day as a 13th+ month.
Unfortunately, you can't just change the formatting, because Excel has already locked those day/month assignments in place, and you just end up moving what Excel THINKS are days and months around visually, not reassigning them.
Frankly, it is surprising to me there is not a date-reverse tool in excel, because I would think this happens all the time. But the formula above does it pretty simply.
NOTE: if your dates don't have leading zeros (i.e. 4/8/11 vs 04/08/12) it gets trickier because you have to extract different amounts of digits depending on the date (i.e. 4/9/11 vs 4/10/11). You then have to build a couple if statements in your formula. Gross.
Here is what worked for me on a mm/dd/yyyy format:
=DATE(VALUE(RIGHT(A1,4)),VALUE(LEFT(A1,2)),VALUE(MID(A1,4,2)))
Convert the cell with the formula to date format and drag the formula down.
Right-click on the column header and select Format Cells, the chose Date and select the desired date format. Those that are not recognized are ambiguous, and as such not interpreted as anything but that is resolved after applying formatting to the column. Note that for me, in Excel 2002 SP3, the dates given above are automatically and correctly interpreted as dates when pasting.
A workaround for this problem consists in temporarily changing your regional settings, so the date format of the CSV imported file "matches" the regional settings one.
Open Office seems to work in a similar way for that issue, see: http://www.oooforum.org/forum/viewtopic.phtml?t=85898
I come across this problem when I tried to convert to Australian date format in excel. I split the cell with delimiter and used the following code from split cells then altered the issue areas.
=date(dd,mm,yy)