无法生成常量日期格式vba

时间:2018-05-24 16:02:35

标签: excel vba excel-vba

我遇到的问题是日期以完全不同的方式生成,导致错误。这些错误包括将年份归类为天数,反之亦然。它将日期格式化为07-JUN-17并将前两个值选为年份,将最后一个值17选为日期。另外,第一次阅读日期不会被覆盖,因为它与2018-1-1完全不同。有关如何更改以创建通用日期格式的任何建议吗?

YearA = Range("YearA").Value + 0
FirstDateRead = DateSerial(YearA + 1, 1, 1)  'Initialize the first Day of the year as the last day
LastDateRead = DateSerial(YearA, 1, 1)       'Initialize the last Day of the year as the first day
FirstDateBill = DateSerial(YearA + 1, 1, 1)  'Initialize the first Day of the year as the last day
LastDateBill = DateSerial(YearA, 1, 1)       'Initialize the last Day of the year as 

       'FirstDate
        Report.Cells(LineNum, 5).NumberFormat = "dd-mmm-yy"
        Report.Cells(LineNum, 5).Value = Format(FirstDateBill, "dd-MMM-yy")
        'LastDate
        Report.Cells(LineNum, 6).NumberFormat = "dd-mmm-yy"
        Report.Cells(LineNum, 6).Value = Format(LastDateBill, "dd-MMM-yy")
        'FirstDate
        Report.Cells(LineNum, 7).NumberFormat = "dd-mmm-yy"
        Report.Cells(LineNum, 7).Value = Format(FirstDateRead, "dd-MMM-yy")
        'LastDate
        Report.Cells(LineNum, 8).NumberFormat = "dd-mmm-yy"
        Report.Cells(LineNum, 8).Value = Format(LastDateRead, "dd-MMM-yy")
        'ProratedDays

1 个答案:

答案 0 :(得分:1)

格式(FirstDateBill," dd-MMM-yy")将日期转换为字符串 " 01-JAN-XX" (其中xx取决于范围(" yearA")

当您将其分配给单元格Report.Cells(LineNum,5)时,Excel会尝试使用其内部(美国)日期转换器将该字符串转换为值。但是你已经将它转换为之前的日期 - 所以停止把它变成一个字符串

ALTER

会正常工作。