首页 > 解决方案 > 将日期写入单元格并更改日期格式

问题描述

我的 VBA 代码有一些问题。我得到了存储为的日期变量,dd/mm/yyyy但是当我将日期写入特定单元格时,格式更改mm/dd/yyyy为和年份。

编码:

Sheets("report_orders").Select
Range("A1").Value = customerName
Range("A2").Value = "äæîðä " & orderID
Range("A3").Value = "äæîðä ì÷åç " & orderPo
'Range("B2").Value = orderDate
Range("B2").Value = Day(orderDate) & "/" & Month(orderDate) & "/" & Year(orderDate)
Debug.Print "year " & Year(orderDate)
Debug.Print "month " & Month(orderDate)
Debug.Print "day " & Day(orderDate)

日期值

但结果是:

在此处输入图像描述

标签: excelvba

解决方案


将日期放入单元格时,可以使用.NumberFormat. 确保在输入值之前格式化单元格。例如

Range("B2").NumberFormat = "mm/dd/yyyy"
Range("B2").Value = orderDate

您可以阅读有关Range.NumberFormat 属性 (Excel).NumberFormat的更多信息


推荐阅读