【问题标题】:vba excel - list files in folder where file created date matches week number and weekday name?vba excel - 列出文件创建日期与周数和工作日名称匹配的文件夹中的文件?
【发布时间】: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,那么它会列出带有星期五日期的文件。

代码还列出了所有文件,而不仅仅是那些日期为星期五的文件。

请谁能告诉我哪里出错了?

谢谢

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    希望我有足够的评论点,但也许这会有所帮助。

    代码在我使用时有效,只是将目标文件夹更改为我计算机上的文件夹。

    您是否尝试使用文件的创建日期或修改日期?此代码仅查看创建日期。

    请注意,星期几的名称区分大小写,因此您可能需要比较当天的大写或小写版本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多