vba Microsoft Excel 使用日期格式复制和粘贴
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19580041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Microsoft Excel Copy and Paste with Format of Date
提问by user2918241
Can anyone help me with copying and pasting but keep the format of the data to be date for all the dates and numbers for all the numbers. So for example I have Customer ID which should be 3 digits (000), and date to be in date format. But when I copy and paste, the date became an weird integer and the ID become 1 digit. Here is the output now
任何人都可以帮助我复制和粘贴,但将数据的格式保留为所有日期的日期和所有数字的数字。例如,我的客户 ID 应该是 3 位数字 (000),日期是日期格式。但是当我复制和粘贴时,日期变成了一个奇怪的整数,而 ID 变成了 1 位数字。这是现在的输出
Copy from:
复制自:
001 AVC BDE 130 6/23/2013
001 AVC BDE 130 6/23/2013
When I paste it becomes:
当我粘贴时,它变成:
Cust ID Name Type Amount Date 1 AVC BDE 130 41448
客户 ID 名称 类型 金额 日期 1 AVC BDE 130 41448
Can anyone help? Here is my code
任何人都可以帮忙吗?这是我的代码
ActiveSheet.Range("A1:F1").Copy
wkbDisplayOrder.Worksheets("Customer's Order").Select
ActiveSheet.Range("A1:F1").PasteSpecial Paste:=xlPasteValues
回答by Portland Runner
Assuming your happy with the formatting that is in the copy fields then you can just do a simple paste instead of paste special. Right now your code is only pasting values because of this line:
假设您对复制字段中的格式感到满意,那么您可以只进行简单的粘贴而不是特殊粘贴。由于这一行,现在您的代码仅粘贴值:
Paste:=xlPasteValues
You can also just change it to this instead:
您也可以将其更改为:
Paste:=xlPasteValuesAndNumberFormats
回答by Santosh
Try this code (UN - Tested)
试试这个代码(联合国 - 测试)
ActiveSheet.Range("A1:F1").Copy
wkbDisplayOrder.Worksheets("Customer's Order").Select
ActiveSheet.Range("A1").NumberFormat = "@"
ActiveSheet.Range("E1").NumberFormat = "MM/dd/yyyy"
ActiveSheet.Range("A1:F1").PasteSpecial Paste:=xlPasteValues