简体   繁体   English

使用Excel VBA拉文件的日期

[英]Pull date of a file using excel vba

I have problem with the date in excel. 我在Excel中的日期有问题。 When i try to pull the date of the file inside a folder, it will pull the hour,min and second although the coding didn't ask for it. 当我尝试将文件的日期拉到一个文件夹中时,尽管编码没有要求,但它将拉动小时,分钟和秒。 Finally i found that the abnormal date in the excel cell show data at alignment right,whereas the correct data show data at alignment left. 最后,我发现excel单元格中的异常日期在右对齐位置显示数据,而正确的数据在左对齐位置显示数据。 This will affect the abnormal sorting problem. 这将影响异常分类问题。 Please help and thanks. 请帮忙,谢谢。

NextRow = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row + 0
For Each objFile In objFolder.files
Sheets("Sheet1").Cells(NextRow, 1).Value = Format(objFile.DateLastModified, "dd/mm/yyyy")
NextRow = NextRow + 1
Next objFile
Sheets("Sheet1").Range("C1").Sort Key1:=Range("C2"), Order1:=xlAscending, Header:=xlYes

Excel: Excel中:

在此处输入图片说明

Visual Basic: Visual Basic:

在此处输入图片说明

Replace "dd/mm/yyyy" with "mm/dd/yyyy" - VBA internally is using the US format! "dd/mm/yyyy"替换为"mm/dd/yyyy" -VBA内部使用的是美国格式!

Alternatively, you can simply store the date value without conversion. 或者,您可以简单地存储日期值而不进行转换。 Internally, Excel will convert your date string back to a date number anyway! 在内部,Excel仍将您的日期字符串转换回日期数字! :-) :-)

Thus, simply use this line: 因此,只需使用以下行:

Sheets("Sheet1").Cells(NextRow, 1) = objFile.DateLastModified

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

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