Excel vba循环浏览文件夹中的新文件

Excel vba循环浏览文件夹中的新文件,excel,vba,Excel,Vba,我有一个从FTP服务器下载CSV文件的文件夹,我需要打开最新的文件并从中提取数据。我这样做是为了从文本连接中获取数据,这是有效的。问题是这个文件夹有15000多个文件,我只需要打开上周的文件,可能是100个文件。 有没有办法按一定顺序打开它们?然后我可以说如果时间戳文件停止循环 谢谢 希望这有帮助。这是我的改造项目。重要的是,您要将希望从中筛选文件的日期写入cell5,1。例如,如果您放置的是2015年11月11日,则只会显示小于该日期的文件,因此不会写入任何文件。但如果将1.1.1914放入单

我有一个从FTP服务器下载CSV文件的文件夹,我需要打开最新的文件并从中提取数据。我这样做是为了从文本连接中获取数据,这是有效的。问题是这个文件夹有15000多个文件,我只需要打开上周的文件,可能是100个文件。 有没有办法按一定顺序打开它们?然后我可以说如果时间戳文件<然后某个时间->停止循环


谢谢

希望这有帮助。这是我的改造项目。重要的是,您要将希望从中筛选文件的日期写入cell5,1。例如,如果您放置的是2015年11月11日,则只会显示小于该日期的文件,因此不会写入任何文件。但如果将1.1.1914放入单元格5,1,则所有文件都将弹出。我不知道你在打开哪个文件。您没有定义类型,所以我没有将工作簿.open放入代码中,但我在代码中放了一个注释,说明应该在哪里打开。因此,请根据您的需要编辑我的代码

Private Sub CommandButton1_Click()
ThisWorkbook.Save
DoEvents
Const ROW_FIRST As Integer = 2
Dim intResult As Integer
Dim strPath As String
Dim objFSO As Object
Dim intCountRows As Integer

Application.FileDialog(msoFileDialogFolderPicker).Title = "Vyberte prosím složku"
Application.FileDialog(msoFileDialogFolderPicker).ButtonName = "Vybrat složku"
Application.FileDialog(msoFileDialogFolderPicker).AllowMultiSelect = True

intResult = Application.FileDialog(msoFileDialogFolderPicker).Show

For Each Item In Application.FileDialog(msoFileDialogFolderPicker).SelectedItems
    If intResult <> 0 Then
        Application.ScreenUpdating = False
        Range("A:A").ClearContents
        Range("B:B").ClearContents
        Range("C:C").ClearContents
        Cells(1, 1).Value = "NAME"
        Cells(1, 2).Value = "PATH"
        Cells(1, 3).Value = "TAIL"
        Cells(1, 4).Value = "Last check:"

        strPath = Application.FileDialog(msoFileDialogFolderPicker).SelectedItems(1)

        Set objFSO = CreateObject("Scripting.FileSystemObject")

        intCountRows = GetAllFiles(strPath, ROW_FIRST, objFSO)
        Call GetAllFolders(strPath, objFSO, intCountRows)
        Application.ScreenUpdating = True
    End If
Next Item
Cells(1, 5).Value = Date
End Sub

Private Function GetAllFiles(ByVal strPath As String, ByVal intRow As Integer, ByRef objFSO As Object) As Integer
DoEvents
Dim objFolder As Object
Dim objFile As Object
Dim i As Integer
i = intRow - ROW_FIRST + 1
Set objFolder = objFSO.GetFolder(strPath)

For Each objFile In objFolder.Files
    inte = InStr(1, objFile.Name, "prázdný")
        If objFile.datecreated > DateValue(Cells(1, 5).Value) Then
'HERE SHOULD BE THE OPENING PROCEDURE!!!!!!!!!!!!!!                
                Cells(i + ROW_FIRST - 1, 1) = objFile.Name
                Cells(i + ROW_FIRST - 1, 2) = objFile.Path
                Cells(i + ROW_FIRST - 1, 3) = Right(objFile.Name, Len(objFile.Name) - InStrRev(objFile.Name, "."))
                i = i + 1
        End If
Next objFile
GetAllFiles = i + ROW_FIRST - 1

End Function

Private Sub GetAllFolders(ByVal strFolder As String, ByRef objFSO As Object, ByRef intRow As Integer)
DoEvents
Dim objFolder As Object
Dim objSubFolder As Object
Static veSpravneSlozce As Boolean

Set objFolder = objFSO.GetFolder(strFolder)
For Each objSubFolder In objFolder.subfolders
        intRow = GetAllFiles(objSubFolder.Path, intRow, objFSO)
        Call GetAllFolders(objSubFolder.Path, objFSO, intRow)
Next objSubFolder
End Sub

开盘程序应在中亚