Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA:在文件系统中循环并查找最新文件_Excel_Vba - Fatal编程技术网

Excel VBA:在文件系统中循环并查找最新文件

Excel VBA:在文件系统中循环并查找最新文件,excel,vba,Excel,Vba,在我的程序中,我想浏览一个复杂的文件结构,并显示其中最新的文件 文件结构有多个文件夹和子文件夹,大部分时间为空。因此,这个宏将有助于揭示最新信息的位置 Sub newestFile() Dim FileSystem as object Dim MostRecentFile as string Dim MostRecentDate as Date Dim FileSpec as String Dim filename as string 'This is where i specify what

在我的程序中,我想浏览一个复杂的文件结构,并显示其中最新的文件

文件结构有多个文件夹和子文件夹,大部分时间为空。因此,这个宏将有助于揭示最新信息的位置

Sub newestFile()
Dim FileSystem as object
Dim MostRecentFile as string
Dim MostRecentDate as Date
Dim FileSpec as String
Dim filename as string

'This is where i specify what type of files i would be looking for
FileSpec ="*.*"

'This is where i specify where the master directory is, so that i may look down into it
Directory ="c:\Directory1\"
filename = Dir(Directory & FileSpec)

set Filesystem = CreateObject("Scripting.FileSystemObject")
Do Folder FileSystem.getFolder(Directory)

set ws = Sheets("Events")
ws.cells(2,7).value = MostRecentFile
ws.cells(2,8).value = MostRecentDate
end sub

private Function DoFolder(Directory)

For each subfolder in Directory.SubFolders
   DoFolder subfolder

Dim file

For each File in Directory.files
    'actions go here
    If File <> "" Then
    MostRecentFile = File
    MostRecentDate = FileDateTime(Directory)
        If FileDateTime(File) > MostRecentDate Then
            MostRecentFile = File
            MostRecentDate = FileDateTime(File)
        End if
    End If
next

next

End Function
子newest文件()
将文件系统作为对象
Dim MostRecentFile作为字符串
Dim MostRecentDate作为日期
Dim FileSpec作为字符串
将文件名设置为字符串
'这是我指定要查找的文件类型的地方
FileSpec=“***”
'这是我指定主目录的位置,以便向下查看它
Directory=“c:\Directory1\”
filename=Dir(目录和文件规范)
设置Filesystem=CreateObject(“Scripting.FileSystemObject”)
Do Folder FileSystem.getFolder(目录)
设置ws=工作表(“事件”)
ws.cells(2,7).value=MostRecentFile
ws.cells(2,8).value=MostRecentDate
端接头
专用函数文件夹(目录)
对于Directory.SubFolders中的每个子文件夹
DoFolder子文件夹
暗文件
对于Directory.files中的每个文件
"行动就在这里
如果文件“”那么
MostRecentFile=File
MostRecentDate=FileDateTime(目录)
如果FileDateTime(File)>MostRecentDate,则
MostRecentFile=File
MostRecentDate=FileDateTime(文件)
如果结束
如果结束
下一个
下一个
端函数
在这段代码中,当代码转到另一个子文件夹时,我总是释放变量(MostRecentFile和MostRecentDate)


我希望得到最新文件(整个结构)的名称和日期。

您需要在模块级别声明变量

Private MostRecentFile as string
Private MostRecentDate as Date

Sub newestFile()
....
End Sub

如前所述,范围当然是一个问题。下面是子程序中的一个循环:

Sub newestFile()
    Dim FileSystem As Object  ' Needed to get file properties
    Set FileSystem = CreateObject("Scripting.FileSystemObject")
    Dim MostRecentDate As Date
    Dim MostRecentFile As String
    Directory = "c:\Directory1\"
    FileSpec = "*.txt"                  '<-- can be "*.xls*" or whatever criteria needed
    MyFile = ""
'Loop through text files in Directory finding the most current file
    MyFile = Dir(Directory & FileSpec)  'Get first file name in directory
    Do While MyFile <> ""
        If MostRecentDate < FileSystem.GetFile(Directory & MyFile).DateLastModified Then
            MostRecentDate = FileSystem.GetFile(Directory & MyFile).DateLastModified
            MostRecentFile = MyFile
        End If
        MyFile = Dir                    'Get next file matching criteria
    Loop
    set ws = Sheets("Events")
    ws.cells(2,7).value = MostRecentFile
    ws.cells(2,8).value = MostRecentDate
End Sub
子newest文件()
将文件系统设置为获取文件属性所需的“对象”
设置FileSystem=CreateObject(“Scripting.FileSystemObject”)
Dim MostRecentDate作为日期
Dim MostRecentFile作为字符串
Directory=“c:\Directory1\”

FileSpec=“*.txt”它们是在子文件中定义的,因此尽管它们是在第二个子文件中设置的,但在超出范围时将被删除。在模块中的第一个子/函数之前声明它们最简单的解决方案是将它们声明为
Global
更优雅的解决方案是通过您的UDF传递它们