Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA循环中时间和日期的分割_Excel_Vba - Fatal编程技术网

Excel VBA循环中时间和日期的分割

Excel VBA循环中时间和日期的分割,excel,vba,Excel,Vba,我有一列数据,其中包含日期和时间信息dd/mm/yyy hh:mm:ss 我试图循环遍历数据列,将日期放入第2列,将时间放入第3列。 下面的代码以正确的数字格式将日期添加到第2列中。但是,第3列中的时间输入为00:00,格式为dd/mm/yyy hh:mm:ss 任何建议都是非常受欢迎的。多谢各位 Dim MyLastRow As Integer Dim MyDateTime As Date 'CREATE THE LOOP START FROM THE FIRST VIABL

我有一列数据,其中包含日期和时间信息dd/mm/yyy hh:mm:ss 我试图循环遍历数据列,将日期放入第2列,将时间放入第3列。 下面的代码以正确的数字格式将日期添加到第2列中。但是,第3列中的时间输入为00:00,格式为dd/mm/yyy hh:mm:ss

任何建议都是非常受欢迎的。多谢各位

Dim MyLastRow As Integer
Dim MyDateTime As Date

    
   'CREATE THE LOOP START FROM THE FIRST VIABLE ROW
MyLastRow = Range("A100000").End(xlUp).Row
 For X = 2 To MyLastRow
MyDateTime = Range("A2").Value
    'ADD ACTIONS
Cells(X + 0, 2).Value = Int(MyDateTime)
Cells(X + 0, 2).NumberFormat = "dd/mm/yyyy"
    
Cells(X + 0, 3).Value = Int(MyDateTime)
Cells(X + 0, 3).NumberFormat = "hh:mm"

'END LOOP
Next

End Sub ```

在Excel中,日期/时间存储为浮点数,其中整数部分表示日期,小数点后的数字表示时间

例如,使用格式
dd/MM/YYYY HH:MM:ss

15/05/2020 11:23:45 = 43966.4748263889
15/05/2020 = 43966
11:23:45 = 0.4748263889
您的代码对这两者都使用整数部分,因此它将日期/时间转换为00:00时的同一日期。那是你的问题。只需将
单元格(X+0,3)。Value=Int(MyDateTime)
更改为
单元格(X+0,3)。Value=CDbl(MyDateTime)-Int(MyDateTime)
以保留时间列中的小数部分

固定代码:

子按钮1\u单击()
Dim MyLastRow作为整数
将MyDateTime设置为日期
'从第一行开始创建循环
MyLastRow=范围(“A100000”)。结束(xlUp)。行
对于X=2到MyLastRow
MyDateTime=范围(“A2”)。值
'添加操作
单元格(X+0,2)。值=Int(MyDateTime)
单元格(X+0,2).NumberFormat=“dd/mm/yyyy”
单元格(X+0,3)。值=CDbl(MyDateTime)-Int(MyDateTime)
单元格(X+0,3).NumberFormat=“hh:mm”
'结束循环
下一个
端接头

在Excel中,日期/时间存储为浮点数,其中整数部分表示日期,小数点后的数字表示时间

例如,使用格式
dd/MM/YYYY HH:MM:ss

15/05/2020 11:23:45 = 43966.4748263889
15/05/2020 = 43966
11:23:45 = 0.4748263889
您的代码对这两者都使用整数部分,因此它将日期/时间转换为00:00时的同一日期。那是你的问题。只需将
单元格(X+0,3)。Value=Int(MyDateTime)
更改为
单元格(X+0,3)。Value=CDbl(MyDateTime)-Int(MyDateTime)
以保留时间列中的小数部分

固定代码:

子按钮1\u单击()
Dim MyLastRow作为整数
将MyDateTime设置为日期
'从第一行开始创建循环
MyLastRow=范围(“A100000”)。结束(xlUp)。行
对于X=2到MyLastRow
MyDateTime=范围(“A2”)。值
'添加操作
单元格(X+0,2)。值=Int(MyDateTime)
单元格(X+0,2).NumberFormat=“dd/mm/yyyy”
单元格(X+0,3)。值=CDbl(MyDateTime)-Int(MyDateTime)
单元格(X+0,3).NumberFormat=“hh:mm”
'结束循环
下一个
端接头

本机函数正好用于此任务:

单元格(X+0,2).Value=DateValue(MyDateTime)
单元格(X+0,3)。值=时间值(MyDateTime)

本机函数正好用于此任务:

单元格(X+0,2).Value=DateValue(MyDateTime)
单元格(X+0,3)。值=时间值(MyDateTime)

用单元格(X+0,3)替换此行单元格(X+0,3)。Value=Int(MyDateTime)用单元格(X+0,3)替换此行单元格(X+0,3)。Value=Int(MyDateTime)用单元格(X+0,3)替换。Value=MyDateTime Int(MyDateTime)谢谢你,把它整理好了。谢谢,整理好了。我不知道。谢谢我不知道。谢谢