复制&在VBA宏中粘贴日期值

时间:2014-09-15 16:23:25

标签: excel vba excel-vba

我编写了一个宏来将一个工作表中的多个单元格值复制并粘贴到另一个工作表中。除了包含日期值的字段外,所有字段都已成功。

例如,当单元格包含' 08-Jan-14'粘贴的值是一个整数' 41647'

如何确保新工作表收到的粘贴值采用日期格式?

Sub place_data(Source As Worksheet, Destination As Worksheet, Optional WorkbookName As String,  
Optional rowToWrite As Integer)

Dim iSourceHeader As Integer
Dim iCol As Integer
Dim lSourceEnd As Long
Dim lDestEnd As Long
Dim rSource As Range
Dim rDestination As Range
Dim rngFrom As Excel.Range
Dim rngTo As Excel.Range

Set rngFrom = Workbooks(WorkbookName).Sheets(Source.Name).Range("D51")
Set rngTo = ThisWorkbook.Sheets("Self Test Summary").Range("A" & rowToWrite)

rngFrom.Copy
rngTo.PasteSpecial Paste:=xlValues

2 个答案:

答案 0 :(得分:6)

您只是粘贴值而不是格式。粘贴后需要再添加一行

rngFrom.Copy
rngTo.PasteSpecial Paste:=xlValues
rngTo.Numberformat = "DD-MMM-YY"

另一种方式

rngTo.Value = rngFrom.Value
rngTo.Numberformat = "DD-MMM-YY"

答案 1 :(得分:2)

取代:

rngFrom.Copy
rngTo.PasteSpecial Paste:=xlValues

使用:

rngFrom.Copy rngTo