2016-12-16 64 views
0

我想列出文件創建日期的文件夾中的所有文件的週數和星期幾名稱與單元格H7和N7中的值匹配。vba excel - 列出文件創建日期與周編號和星期幾名稱相匹配的文件夾中的文件?

例如我的文件夾中包含下列文件:

1.PDF (Created 16/12/2016) 
2.PDF (Created 01/12/2016) 
3.PDF (Created 16/12/2016) 

我的手機H7包含星期數50(12月15日將在下降)。

我的手機N7包含星期五的星期五(這將匹配12月16日)。

我使用的代碼如下:

Sub Example1() 
Dim objFSO As Object 
Dim objFolder As Object 
Dim objFile As Object 
Dim i As Integer 



'Create an instance of the FileSystemObject 
Set objFSO = CreateObject("Scripting.FileSystemObject") 
'Get the folder object 
If Dir("G:\STOCK\(3) Promotions\Allocations\" & Range("T7").Value & "\" & client, vbDirectory) <> "" Then 


Set objFolder = objFSO.GetFolder("G:\STOCK\(3) Promotions\Allocations\" & Range("T7").Value & "\") 




'Utilize Adv Day = False 
If Range("N7").Value <> "" Then 


i = 1 
'loops through each file in the directory and prints their names and path 
For Each objFile In objFolder.Files 


If DatePart("ww", objFile.DateCreated, vbMonday, vbFirstFourDays) = Range("H7").Value And WeekdayName(Weekday(objFile.DateCreated)) = Range("N7").Value Then 


'print file PG 
    Cells(i + 12, 1) = Range("T7").Value 
    'print file Month 
    Cells(i + 12, 5) = Range("H7").Value 

    'print file Year 
    Cells(i + 12, 9) = Range("B7").Value 

    'print file name 
    Cells(i + 12, 13) = objFile.Name 

    'print file path 
    Cells(i + 12, 18) = "=hyperlink(""" & objFile.Path & """)" 

    i = i + 1 
    End If 

Next objFile 

Else 

MsgBox "No Results Found." 

End If 

End If 

End Sub 

的代碼似乎並沒有正常工作,如果我把在週六進入細胞N7則列出了星期五的日期的文件。

此外代碼列出了所有文件,而不僅僅是那些星期五的日期文件。

請有人能告訴我我要去哪裏嗎?

感謝

回答

0

希望我有足夠的積分,發表評論,但也許這將是有益的。

當我使用它時,只需將目標文件夾更改爲我的計算機上的文件夾即可使用該代碼。

你想使用文件的創建日期或修改日期嗎?此代碼僅查看創建的日期。

請注意,星期幾名稱區分大小寫,因此您可能需要比較當天的大寫或小寫版本。

相關問題