【发布时间】:2016-12-16 17:20:58
【问题描述】:
我正在尝试列出文件夹中的所有文件,其中文件创建日期的周数和工作日名称与单元格 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,那么它会列出带有星期五日期的文件。
代码还列出了所有文件,而不仅仅是那些日期为星期五的文件。
请谁能告诉我哪里出错了?
谢谢
【问题讨论】: