gpt4 book ai didi

vba - 在 VBA 宏中复制和粘贴日期值

转载 作者:行者123 更新时间:2023-12-02 09:48:39 25 4
gpt4 key购买 nike

我编写了一个宏,用于将多个单元格值从一个工作表复制并粘贴到另一个工作表。除包含日期值的字段外,此操作在所有字段上均成功。

例如,当单元格包含“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

最佳答案

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

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

另一种方式

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

关于vba - 在 VBA 宏中复制和粘贴日期值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25852538/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com