简体   繁体   English

将日期转换为数字格式VBA

[英]Convert date to number format vba

I'm having issues copying a date from one file to another and preserving the format (dd/mm/yyyy) while using vba. 我在使用vba时将日期从一个文件复制到另一个文件并保留格式(dd / mm / yyyy)时遇到问题。 A straight copy results in the dates that can be converted to US date format changing to mm/dd/yyyy and those that cannot (any date with dd > 12) changing to a string. 连续副本会导致将日期转换为美国日期格式,将其更改为mm / dd / yyyy,而将日期(dd> 12的任何日期)更改为字符串。 I have tried changing the date to a number first and then copy/paste (with the plan to go back to date format once the copy was complete) but this only changes format for the dates with dd<=12, the dates with dd>12 still come across as string format. 我尝试过先将日期更改为数字,然后再复制/粘贴(复制完成后计划返回日期格式),但这只会更改dd <= 12的日期和dd>的日期的格式12仍然以字符串格式出现。 Also the dates that successfully change to numbers seem to change to US format fist then to number format so I end up with the wrong date anyway The strange thing is going through these steps manually (outside of vba) works fine, I can get all the dates into whatever format I want and the US format never appears. 同样,成功更改为数字的日期似乎先更改为美国格式,然后更改为数字格式,所以无论如何我都会遇到错误的日期。奇怪的是,手动执行这些步骤(在vba之外)可以正常工作,我可以可以使用我想要的任何格式,而美国格式则永远不会出现。 Here is the code I have been using: 这是我一直在使用的代码:

Sheets("Import").Select
PathName = Range("B2").Value
Filename = Range("B3").Value
TabName = Range("B4").Value
ControlFile = ActiveWorkbook.Name
Workbooks.Open Filename:=PathName & Filename
ActiveSheet.Name = TabName
Columns("A:A").Select
Selection.NumberFormat = "0"
Range("A2:A200").Copy Destination:=Workbooks(ControlFile).Sheets("Import").Range("A8:A206")

Any help or suggestions for things to try would be great. 对尝试的事情有任何帮助或建议都很好。 Cam 凸轮

I would try one of these. 我会尝试其中之一。 I am confused to which format your wanting. 我对您想要的格式感到困惑。

Workbooks.Open Filename:=PathName & Filename
ActiveSheet.Name = TabName
Range("A2:A200").Copy Destination:=Workbooks(ControlFile).Sheets("Import").Range("A8:A206")

'Workbooks(ControlFile).Sheets("Import").Columns("A:A").NumberFormat = "dd/mm/yy;@" 

I am sure one of these will work 我确信其中之一会起作用

'Workbooks(ControlFile).Sheets("Import").Columns("A:A").NumberFormat = "mm/dd/yy;@" 

If all else fails just run this after your code and make it any format your seeking. 如果所有其他方法均失败,则在代码后运行此代码,并将其设置为您想要的任何格式。

Sub Change_Date()
Dim oCell As Range
Dim LastRow As Long

LastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row

For Each oCell In ActiveSheet.Range("A1:A" & LastRow)
    oCell.Value = Format(Trim(oCell.Value), "dd/mm/yyyy")
Next oCell


End Sub

Living in Japan and having to deal with weird date conversions I have learnt the only real safe way to keep the formatting the way you want is to use NumberFormatLocal to set the cell the way you need it. 我生活在日本,必须处理奇怪的日期转换,我了解到,保持所需格式唯一真正安全的方法是使用NumberFormatLocal来按需要设置单元格。 No matter how you format the date in the VBA once it is in the cell, Excel seems to take over from what the local settings are set to. 不论日期如何在单元格中格式化后,无论如何在VBA中格式化日期,Excel似乎都将取代本地设置所设置的内容。

The below code is what I would use in your situation to guarantee the format that you want. 以下代码是我在您所处的情况下用来保证所需格式的代码。

   Sheets("Import").Select
   PathName = Range("B2").Value
   Filename = Range("B3").Value
   TabName = Range("B4").Value
   ControlFile = ActiveWorkbook.Name
   Workbooks.Open Filename:=PathName & Filename
   ActiveSheet.Name = TabName
   Columns("A:A").Select
   Selection.NumberFormat = "0"
   Range("A2:A200").Copy Destination:=Workbooks(ControlFile).Sheets("Import").Range("A8:A206")

   Workbooks(ControlFile).Sheets("Import").Range("A8:A206").NumberFormatLocal = "d/m/yyyy"

You could also use something like this on a date 1/12/2016 您也可以在1/12/2016年1月12日使用这样的内容

Range("A8:A206").NumberFormatLocal = "dd/mmm/yyyy"

To give you an output of, 为了给您输出

12/Jan/2016 instead of Jan/12/2016 12/Jan/2016 Jan/12/2016 12/Jan/2016而不是12/Jan/2016 Jan/12/2016

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM