简体   繁体   English

vba excel-列出文件夹中文件创建日期与星期几和星期几名称匹配的文件吗?

[英]vba excel - list files in folder where file created date matches week number and weekday name?

I am trying to list all files in a folder where the file created date has a week number and weekday name that matches the values in cells H7 and N7. 我正在尝试列出文件夹中的所有文件,在该文件夹中,文件创建日期的星期编号和工作日名称与单元格H7和N7中的值匹配。

For instance my folder contains the following files: 例如,我的文件夹包含以下文件:

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

My cell H7 contains the week number 50 (which 15th December would fall under). 我的单元格H7包含第50周(属于12月15日)。

My cell N7 contains the weekday Friday (which would match the 16th december). 我的N7单元格包含星期五(与12月16日匹配)的工作日。

The code i am using is below: 我正在使用的代码如下:

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

The code doesn't seem to work correctly, if i put in Saturday into cell N7 then it lists the files with friday's date. 该代码似乎无法正常工作,如果我将星期六放在N7单元格中,则会列出带有星期五日期的文件。

Also the code is listing all files and not just those files with Friday's date. 代码还列出了所有文件,而不仅仅是列出了周五日期的那些文件。

Please can someone show me where i am going wrong? 请有人能告诉我我要去哪里错吗?

Thanks 谢谢

Wish I had enough points to comment, but maybe this will be helpful. 希望我有足够的意见要发表意见,但这也许会有所帮助。

The code worked when I used it, just changing the target folder to a folder on my computer. 当我使用它时,代码起作用了,只是将目标文件夹更改为计算机上的文件夹。

Are you trying to use the date the file was created or the date it was modified? 您是否要使用文件创建或修改的日期? This code is looking only at the created date. 该代码仅在创建日期查找。

Note that the day-of-week name is case sensitive, so you might want to compare upper-case or lower-case versions of the day. 请注意,星期几名称区分大小写,因此您可能需要比较当天的大写或小写版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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