Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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
VBA循环目录_Vba_Loops_Excel_Directory - Fatal编程技术网

VBA循环目录

VBA循环目录,vba,loops,excel,directory,Vba,Loops,Excel,Directory,**大家好 我想在下面的脚本中加入搜索文件和仅导出文件夹中最新文件的数据的功能。我将每周向文件夹中添加一个新文件,因此不希望跨文件夹复制旧数据区域 有人能帮忙吗** 如果使用,则可以使用属性来完成。下面的代码应该可以帮助您开始: 未经测试 Dim FSO As FileSystemObject Dim objFile As File Dim myFolder Dim strFilename As String Dim dtFile As Date 'set folder location C

**大家好

我想在下面的脚本中加入搜索文件和仅导出文件夹中最新文件的数据的功能。我将每周向文件夹中添加一个新文件,因此不希望跨文件夹复制旧数据区域

有人能帮忙吗**


如果使用,则可以使用属性来完成。下面的代码应该可以帮助您开始:

未经测试

Dim FSO As FileSystemObject
Dim objFile As File
Dim myFolder
Dim strFilename As String
Dim dtFile As Date

'set folder location
Const myDir As String = "C:\Users\ramandeepm\Desktop\consolidate"

'set up filesys objects
Set FSO = New FileSystemObject
Set myFolder = FSO.GetFolder(myDir)

'loop through each file and get date last modified. If largest date then store Filename
dtFile = DateSerial(1900, 1, 1)
For Each objFile In myFolder.Files
    If Len(objFile.Name) > 7 Then
        If objFile.DateLastModified > dtFile Then
            dtFile = objFile.DateLastModified
            strFilename = objFile.Name
        End If
    End If
Next objFile
Workbooks.Open strFilename
注意:此代码正在查找最近修改的日期。因此,只有在对文件夹中的其他文件进行任何修改后才创建最新文件时,此操作才有效。此外,您可能需要。

请参见此处:
Dim FSO As FileSystemObject
Dim objFile As File
Dim myFolder
Dim strFilename As String
Dim dtFile As Date

'set folder location
Const myDir As String = "C:\Users\ramandeepm\Desktop\consolidate"

'set up filesys objects
Set FSO = New FileSystemObject
Set myFolder = FSO.GetFolder(myDir)

'loop through each file and get date last modified. If largest date then store Filename
dtFile = DateSerial(1900, 1, 1)
For Each objFile In myFolder.Files
    If Len(objFile.Name) > 7 Then
        If objFile.DateLastModified > dtFile Then
            dtFile = objFile.DateLastModified
            strFilename = objFile.Name
        End If
    End If
Next objFile
Workbooks.Open strFilename