简体   繁体   中英

Pull date of a file using excel vba

I have problem with the date in 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. 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:

在此处输入图片说明

Visual Basic:

在此处输入图片说明

Replace "dd/mm/yyyy" with "mm/dd/yyyy" - VBA internally is using the US format!

Alternatively, you can simply store the date value without conversion. Internally, Excel will convert your date string back to a date number anyway! :-)

Thus, simply use this line:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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