我正在尝试列出文件夹中的所有文件,其中文件创建日期的周数和工作日名称与单元格H7和N7中的值匹配。
例如,我的文件夹包含以下文件:
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 :(得分:0)
希望我有足够的评论意见,但也许这会有所帮助。
我使用它时代码有效,只需将目标文件夹更改为计算机上的文件夹即可。
您是否尝试使用创建文件的日期或修改日期?此代码仅查看创建日期。
请注意,星期几名称区分大小写,因此您可能希望比较当天的大写或小写版本。