英文:
Excel Date formatting function not working
问题
以下是已翻译的内容:
有两种格式,一种是 yyyy-mm-dd,另一种是 mm/dd/yyyy。
我尝试使用“文本分列”和Ctrl+1来更改日期格式为yyyy-mm-dd,但这两种方法都不起作用。
而且,我发现如果尝试复制数据,粘贴为数值,它会显示<br>
期望的 yyyy-mm-dd 将显示为 5 位数值,但 mm/dd/yyyy 不会改变。
英文:
So here is my original data<br>
there are two formats, one is yyyy-mm-dd and another one is mm/dd/yyyy.
I tried to use "Text to Column" and Contr+1 to change the format to yyyy-mm-dd, none of these two method working.
And also, I find if I try to copy the data, paste as value, it will shows<br>
The desirable one yyyy-mm-dd will present as 5 digit numeric value but mm/dd/yyyy won't change.
答案1
得分: 0
使用您想要的格式化数字选项。到达方法:
- 它位于“主页” > 数字部分,选择下拉菜单 > “日期”或“自定义”。
- 右键单击单元格,选择“格式单元格” > “数字” > “日期”或“自定义”。
- 找到一个具有正确格式的单元格,然后单击“主页” > “格式刷”,然后单击具有错误格式的单元格。
默认格式取决于您设置软件时的国家/地区。在美国,“日期”是MM/DD/YYYY,“自定义”是您选择的方法,例如YYYY-MM-DD。
根据Williams的评论进行编辑
检查值是否具有前导或尾随空格或其他不可打印字符。如果存在这种字符,您可能需要将其删除。如果没有空格字符问题,那么这些值可能以文本类型的形式进入。要更改为数字,请执行以下操作:
- 在空单元格中键入
1
,然后复制该单元格。 - 高亮显示所有日期和数字。右键单击高亮显示的单元格。
- 在菜单中选择“粘贴特殊...” > “粘贴特殊...”
- 应该出现一个新菜单,其中选择“操作” > “乘法”。这将将文本值更改为数字值。
英文:
Use the formatting number option that you want. Ways to get there:
- It is under the "Home" > Number section choose the drop down > "Date" or "Custom"
- right click on the cell and choose "format cell" > "Number" > "Date" or "Custom"
- Find a cell that has the right format and click on "Home" > "Format Painter" then click on the cell that has wrong format.
The default format depends on your country when you setup the software. In the USA the "Date" is MM/DD/YYYY and "Custom" is the method you choose like YYYY-MM-DD
Edit - based on Williams comment
Check if the values have a proceeding or trailing space or other none printing character. If there is one that exist you will probably need to remove it. If you don't have the space character problem then the values are probably coming in as a text type. To change to a number do the following:
- Type
1
in an empty cell then copy the cell. - highlight all the dates and numbers. Right click on the highlighted cells.
- In the menu choose "Paste Special..." > "Paste Special..."
- A new menu should appear that has "Operation" choose "Multiply". This will change text values to number values.
答案2
得分: 0
你的困难是基于Excel如何确定它所查看的内容。
当它在一列中找到数据,这些数据只有足够的字符以成为您系统的日期格式之一,并且只包含适合其输出的字符(所以,如果出现"}"字符之类的字符,可能不会这样),它将尝试将该列中的所有值解释为日期。它只查看一列的前八个值来决定是否对整个列执行此操作。(这是可以更改的,但很难。)
对于根据您计算机操作系统的默认格式设置(而不是Excel的任何设置,而是您的操作系统设置)进行解释时不是日期的任何内容都将按原样传递并在打开的文件中视为文本。
因此,任何不能转换为日期的内容在更改数字格式时不会发生变化,因为...它本来就不是数字,不是Excel认为的数字。
更糟糕的是,有些内容会被错误地转换,而您甚至没有预料到。例如,您的系统似乎设置了尝试使用"DD/MM/YYYY"(以及其他格式,这些格式可能工作得很好或者存在与此类问题本身相同的问题)进行解释的值。因此,"3/25/1964"将月份解释为"25",并且无法成功转换,结果是文本。但是,根据系统的DD/MM/yyyy格式,1964年1月8日或者8/1/1964在MM/DD/yyyy格式中有一个错误的对应。因此,如果您的数据中包含了8/1/1964这样的日期,就像我在美国这里可能输入的那样,您的Excel将其解释为1月8日,而不是8月1日。由于这很少是要查找的错误类型...您在某一天之前都不会注意到它。
针对上述问题,除非数据包含可以让您知道已发生错误导入的信息,否则无法做太多事情。但是,如果您知道数据永远不会出现这种问题,那么您可以忽略它。
在您提供的数据中,而不会增加像上面提到的那些问题,唯一一种似乎无法导入为日期而导入为字符串的日期是实际上是CSV文件中格式为"MM/DD/yyyy"的输入。由于这是唯一一种不需要处理多种格式的情况,因此问题实际上相当简单。
可以在其他地方使用一个公式。通常称为"辅助列",它甚至不必位于数据所在的工作表上。您只需插入一个用于处理的工作表,然后将结果转移,然后删除辅助工作表。
要使用的公式将执行两项操作:1)检查是否存在问题,即导入的值是否实际上是日期,或者不是日期,2)如果尚不是日期,则将其分解为部分并从这些部分创建日期。它使用较新的TEXTBEFORE
和TEXTAFTER
函数,但如果需要,也可以使用较旧的函数来执行此操作。
=IF(ISNUMBER(A1), A1, DATE(TEXTAFTER(A1,"/",""), TEXTBEFORE(TEXTAFTER(A1,"/"),"/"), TEXTBEFORE(A1,"/")))
ISNUMBER
用于检查单元格中的值是否为数字。Excel导入的任何日期实际上都是一个按每天递增1的序列号。因此,此函数可以识别所有已正确导入的值,IF
将其作为结果返回。
如果测试失败,DATE
函数将采用从字符串分解为部分获取的三个部分,并创建一个Excel日期值。
现在,您有一个包含所有导入的数据以实际日期值输出的范围。复制所有数据,并将其"粘贴为值"到原始范围上。如果您不希望继续存在辅助范围/列/工作表/任何内容,可以删除它(通常情况下不需要它)。
但是...请查看导致CSV文件的日期创建。了解日期条目的变化是否包括人们使用MM/DD/yyyy和DD/MM/yyyy的方法,因为并非所有Excel和操作系统可以使用MM/DD的条目都能正确导入。
英文:
Your difficulty is based on how Excel figures what it is looking at.
When it finds data in a column that has only enough characters to be one of your system's date formats, and only appropriate characters to their outputs (so, probably not if a "}" character appears in it, that kind of thing), it will try to interpret all values in the column as dates. It only looks at the first eight values for a column in deciding to do this for the whole column. (That can be changed, but it's "hard.)
Anything that is not a date when interpreted against your computer's operating system's default formatting choices (NOT Excel's settings of any kind, but your OS settings), will be passed as in the source and treated as text in the opened file.
Consequently, anything that does not translate to a date will not change when changing number formats because... it is not a number to begin with, not to Excel.
Worse, by the way, some things will translate, but wrongly, and you don't even expect that. For instance, your system seems to have a setting these values are tried against of "DD/MM/YYYY" (and others, which may work nicely or have this exact problem themselves). So "3/25/1964" presents the month as being "25" and it fails, coming through as text. But January 8 1964, or 8/1/1964 in the DD/MM/yyyy format of the system has an evil twin in the MM/DD/yyyy format. So if your data has August 1 1964 entered in it as 8/1/1964, as I might enter it here in the US, your Excel will bring it in as January 8, not August 1. And since this is seldom the kind of error looked for... you never notice it, until someday it matters.
You cannot do a lot about the above, unless the data contains information that lets you know a mistaken import has occurred. But if you know the data will never have that kind of problem, you can ignore it.
In just the data you provide, and not adding to your troubles with things like the above, the one kind of date you do not seem to be able to import as a date rather than a string is the kind that is actually a "MM/DD/yyyy" formatted entry in the CSV file. Since this is the only kind, that you do not have to deal with several kinds, you do not have the possibly extreme problem of separating them into kinds. So the problem is pretty easy, really.
Use a formula in another place. Often called a "helper column" it need not even be on the sheet your data is on. You can just insert a sheet to work on, then transfer the results, and delete the helper sheet.
The formula to use will do two things: 1) Check to see if there is a problem at all, i.e., is the value imported as an actual date, or is it not, and 2) If it is not already a date, decompose it into parts and create a date out of the parts. It uses the newer TEXTBEFORE
and TEXTAFTER
functions but there are easy ways to use older functions for this if needed.
=IF( ISNUMBER( A1 ), A1, DATE( TEXTAFTER(A1,"/",2), TEXTBEFORE(TEXTAFTER(A1,"/"),"/"), TEXTBEFORE(A1,"/") ))
ISNUMBER
checks to see if the value in the cell is a number. Anything Excel has imported as a date will actually be a serial number that counts up by 1 each further day. So this function can identify all nicely imported values and the IF
will just give that as the result.
If the test fails, the DATE
function takes the three pieces gotten from decomposing the string into parts and creates an Excel date value.
You now have a range with all the imported data output as real date values. Copy it all, and Paste|Special|Values
it onto the original range. Delete the helper range/column/sheet/whatever if you do not wish it to continue to exist (often one doesn't).
But... look into the date creation that led to the CSV file. Find out if the variation in date entries could include people entering in MM/DD/yyyy and DD/MM/yyyy methods because not all entries that Excel and the OS can make work in MM/DD will turn out to import correctly.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论