在VBA中粘贴时的日期格式

时间:2018-05-03 15:58:14

标签: excel vba date format

下面的代码只是复制和粘贴日期,通常采用mm / dd / yy格式,但可以是任何日期。

WB1.Worksheets("WS1").Range(Cells(3, "g"), Cells(RowCountOPR, "g")).Copy
WB2.Worksheets("WS2").Cells(RowCountTrack, "c").PasteSpecial
WB2.Worksheets("WS2").Cells(RowCountTrack, "n").PasteSpecial
WB2.Worksheets("WS2").Cells(RowCountTrack, "t").PasteSpecial  

我希望它在“c”列中粘贴yyyymmdd的格式。如何在此处添加格式?我可以吗?

1 个答案:

答案 0 :(得分:0)

只需添加一些更改单元格格式的行:

WB1.Worksheets("WS1").Range(Cells(3, "g"), Cells(RowCountOPR, "g")).Copy
WB2.Worksheets("WS2").Cells(RowCountTrack, "c").PasteSpecial
WB2.Worksheets("WS2").Cells(RowCountTrack, "n").PasteSpecial
WB2.Worksheets("WS2").Cells(RowCountTrack, "t").PasteSpecial 
WB2.Worksheets("WS2").Cells(RowCountTrack, "c").NumberFormat = "yyyymmdd"
WB2.Worksheets("WS2").Cells(RowCountTrack + 1, "c").NumberFormat = "yyyymmdd"