简体   繁体   English

将日期和时间加入Excel / VBA中的单个单元格

[英]Joining a date and time into a single cell in Excel / VBA

I have to report basically the same information from inspections to two different clients, both of whom have provided me with an Excel spreadsheet in their own preferred format, and password protected. 我必须将检查中基本相同的信息报告给两个不同的客户,这两个客户都以自己的首选格式向我提供了Excel电子表格,并且密码受到保护。 I have put the two sheets into my own workbook, and have managed to get 'almost' all the data on both sheets to populate from my own data. 我已将这两张纸放入我自己的工作簿中,并设法将“几乎”两张纸上的所有数据都填入我自己的数据中。 Where I am stuck though is on the time logs. 虽然在时间日志上我被卡住了。

Client 'A' has each time and date in a single column, in the format "12.29.12 14:30". 客户'A'在单个列中包含每个时间和日期,格式为“12.29.12 14:30”。 Client 'B' has two columns, date as "12/29/12" in the first, and time as "1430" in the second. 客户'B'有两列,第一列为“12/29/12”,第二列为“1430”。

I'm trying to avoid having to type all the same dates and times twice - it can be several dozen lines - both to save effort and to avoid errors. 我试图避免必须两次输入所有相同的日期和时间 - 它可以是几十行 - 既省力又避免错误。 What I really need to do is either concatenate the date and time from client 'B's report and put it into client 'A's, or split the date and time up in 'A's so I can put it ito 'B's. 我真正需要做的是将客户'B报告中的日期和时间连接起来并将其放入客户'A'中,或者将日期和时间分成'A',这样我就可以把它放到'B'上。 I've tried several approaches, but just end up with error codes or meaningless numbers. 我尝试了几种方法,但最终得到的错误代码或无意义的数字。

Can anyone point me in the right direction? 谁能指出我正确的方向?

Thanks, Richard 谢谢,理查德

Never mind... the very next thing I tried worked :-) 没关系......我尝试过的下一件事:-)

To anyone else reading this, the trick is not to concatentate the date and time values, but to add them together. 对于阅读此内容的任何人来说,诀窍不是连接日期和时间值,而是将它们添加到一起。

Richard 理查德

As you later pointed out in your own question, in order to manipulate date/times in Excel, you should add instead of concatenating. 正如您稍后在自己的问题中指出的那样,为了在Excel中操作日期/时间,您应该添加而不是连接。

The reason for this is that Excel stores all date/times as a number representing the number of days since January 1, 1900*. 原因是Excel将所有日期/时间存储为表示自1900年1月1日以来的天数的数字*。 This number is stored as a 8-byte double. 此数字存储为8字节的双精度数。

Use Excel number formats to display the date/time format that you desire. 使用Excel数字格式显示所需的日期/时间格式。

If you find in a worksheet that some cell's dates are in fact stored as text, use the DATEVALUE function. 如果在工作表中发现某些单元格的日期实际上存储为文本,请使用DATEVALUE函数。

*By default, Excel 2010 for Windows uses the number of days since 1900. There is an option to use the number of days since 1904 for compatibility with other versions. *默认情况下,Excel 2010 for Windows使用自1900年以来的天数。可以选择使用自1904年以来与其他版本兼容的天数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM