如何使用VBA连接日期(mm / dd / yyyy)与时间(hh:mm:ss) [英] How to concatenate date(mm/dd/yyyy) with time (hh:mm:ss) using VBA

查看:446
本文介绍了如何使用VBA连接日期(mm / dd / yyyy)与时间(hh:mm:ss)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个单元格:A1和B1




  • A1的格式为 mm / dd / yyyy


  • B1的格式为 hh:mm:ss




当使用VBA连接两个单元格A1和B1时,输出的格式为 mm / dd / yyyy hh:mm 。为什么秒钟不显示?如何解决这个问题?



注意:



a)连接后,输出将复制到A1。 b)我尝试将日期格式更改为 dd / mm / yyyy ,在这种情况下它的工作正常。

解决方案

很难说出你在做什么,没有看到你的代码,但我认为你指定的价值的格式您正在写入单元格A1:使用VBA 格式函数将日期时间转换为字符串。



但是,要更改单元格中显示日期的方式,您需要更改该单元本身的格式 / em>(不是写入的格式)。否则Excel可能会解释单元格中的值,并将其重新捕捉到该单元格的指定数字格式。



您可以在Excel 2010窗口中更改单元格的格式,如下所示:Home> Number> Custom> Type: mm / dd / yy hh: mm:ss



或使用VBA:

  Range(A1)。NumberFormat =mm / dd / yy hh:mm:ss


I have two cells: A1 and B1

  • A1 has data in format of mm/dd/yyyy

  • B1 has data in format of hh:mm:ss

When I concatenate the two cells A1 and B1 using VBA, the output contains has the format mm/dd/yyyy hh:mm. Why are the seconds not displaying? How can I fix this?

Note :

a) After concatenation the output is copied to A1.

b) I tried changing the format of date to dd/mm/yyyy, in this case its working fine .

解决方案

It's hard to tell what you are doing exactly without seeing your code, but I think you are specifying the format of the value you are writing to cell A1: using the VBA Format function you are converting the date-time into a string.

However, to change the way the date is displayed in a cell, you need to change the format of that cell itself (not the format of the value written to it). Otherwise Excel will likely interpret the value in the cell and snap it back to the specified number format for that cell.

You can change the cell's format in the Excel 2010 window like this: Home > Number > Custom > Type: mm/dd/yy hh:mm:ss

Or, using VBA:

Range("A1").NumberFormat = "mm/dd/yy hh:mm:ss"

这篇关于如何使用VBA连接日期(mm / dd / yyyy)与时间(hh:mm:ss)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆