Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.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
VBA Excel 2013格式函数更改日期_Vba_Excel_Ms Office - Fatal编程技术网

VBA Excel 2013格式函数更改日期

VBA Excel 2013格式函数更改日期,vba,excel,ms-office,Vba,Excel,Ms Office,下面的代码行不仅更改了日期的格式,还更改了日期的值。为什么会发生这种情况?我如何避免 代码: rng.Cells的值(intStartRow,rng.Columns.Count+1)。执行代码前的值: rng.Cells的值(intStartRow,rng.Columns.Count+1)。执行代码后的值: 如果您希望转换为实际日期并更改显示格式,请使用: With rng.Cells(intStartRow, rng.Columns.Count + 1) .Value = CDate(

下面的代码行不仅更改了日期的格式,还更改了日期的值。为什么会发生这种情况?我如何避免

代码:

rng.Cells的值(intStartRow,rng.Columns.Count+1)。执行代码前的值

rng.Cells的值(intStartRow,rng.Columns.Count+1)。执行代码后的值


如果您希望转换为实际日期并更改显示格式,请使用:

With rng.Cells(intStartRow, rng.Columns.Count + 1)
   .Value = CDate(.Value)
   .NumberFormat = "mmm-yy"
End With

如果要转换为实际日期并更改显示格式,请使用:

With rng.Cells(intStartRow, rng.Columns.Count + 1)
   .Value = CDate(.Value)
   .NumberFormat = "mmm-yy"
End With

发生这种情况的原因是,日期信息在内部存储为十进制数,并以应用于单元格的日期格式显示。 在代码中,您将日期信息更改为字符串,因为函数
Format()
始终是字符串。
Rory提供了解决该问题的解决方案。发生这种情况的原因是,日期信息在内部存储为十进制数,并以应用于单元格的日期格式显示。 在代码中,您将日期信息更改为字符串,因为函数
Format()
始终是字符串。
Rory提供了解决此问题的解决方案。
Format
过程将日期转换为字符串。将其写回单元格会通过猜测格式将其转换为日期(这是不正确的,因为年份现在是两位数字)。要将其保留为字符串,可以在前面添加引号:
Cells(…).Value=“””&Format(…)
Format
过程将日期转换为字符串。将其写回单元格会通过猜测格式将其转换为日期(这是不正确的,因为年份现在是两位数字)。要将其保留为字符串,可以在前面添加引号:
Cells(…).Value=“””&Format(…)