在文件夹中打开最近的.csv文件

时间:2018-02-28 19:29:20

标签: vba

我对VBA很新,但我正在努力学习更多东西。现在我正在尝试编写一个宏来打开我的最新文件:Z驱动器是一个逗号分隔文件(.CSV)。以下代码不起作用,但我想知道是否有人有任何建议? 谢谢你的帮助!

Sub NewestFile()
Dim MyPath As String
Dim MyFile As String
Dim LatestFile As String
Dim LatestDate As Date
Dim LMD As Date

MyPath = "Z:\"
If Right(MyPath, 1) <> “ \ ” Then MyPath = MyPath & “ \ ”
  MyFile = Dir(MyPath & “ * .csv”, vbNormal)
  If Len(MyFile) = 0 Then
    MsgBox “No files were found…”, vbExclamation
    Exit Sub
  End If
  Do While Len(MyFile) > 0
    LMD = FileDateTime(MyPath & MyFile)
    If LMD > LatestDate Then
      LatestFile = MyFile
      LatestDate = LMD
    End If
    MyFile = Dir
  Loop
  Workbooks.Open MyPath & LatestFile
End Sub

1 个答案:

答案 0 :(得分:0)

你走了。版本1我只是使用msgbox来显示文件夹中的最后一个修改过的csv。版本2打开文件并使用filedialog,因为来自fso.GetFolder的文件路径存在OP困难。

添加对MS Scripting runtime(工具&gt;参考)的引用,然后

Sub GetLastModifiedCSV()

   'Early binding code. Requires reference to MS Scripting Runtime
    Dim fso As Scripting.FileSystemObject     
    Set fso = New Scripting.FileSystemObject

   'Late binding code. To be used instead of two lines above if "user-defined type not defined" /No reference added. You would uncomment line below.

  'Dim fso As Object: Set fso = CreateObject("Scripting.FileSystemObject") 

    Dim myFolder As Object

    Set myFolder = fso.GetFolder("C:\Users\User\Desktop\Test")

    Dim currentFile As Object
    Dim maxFileName As String
    Dim maxDate As Date

    For Each currentFile In myFolder.Files

        If fso.GetExtensionName(currentFile) = "csv" Then

            If currentFile.DateLastModified > maxDate Then
                 maxDate = currentFile.DateLastModified
                 maxFileName = currentFile.Name
            End If

        End If

    Next currentFile

   Msgbox maxFileName

End Sub

其他参考资料:

1)How to get the last modified file in a directory using VBA in Excel 2010

2)Using VBA FileSystemObject, specific file File extension

3)File system object explained

4)msoFileDialogFolderPicker

版本2使用FileDialog获取GetFolder的文件夹路径:

Option Explicit

Public Sub GetLastModifiedCSV()

    Const folderPath As String = "C:\Users\User\Desktop\Test"

   'Early binding code. Requires reference to MS Scripting Runtime
    Dim fso As FileSystemObject
    Set fso = New FileSystemObject

   'Late binding code. To be used instead of two lines above if "user-defined type not defined" /No reference added. You would uncomment line below.

  'Dim fso As Object: Set fso = CreateObject("FileSystemObject")

    Dim myFolder As Object
    Dim currentFile As Object
    Dim maxFileName As String
    Dim maxDate As Date

    With Application.FileDialog(msoFileDialogFolderPicker)

    If .Show Then
      Set myFolder = fso.GetFolder(.SelectedItems(1))  ' & "\" 
    Else
        Exit Sub
    End If

    End With

    For Each currentFile In myFolder.Files

        If fso.GetExtensionName(currentFile) = "csv" Then

            If currentFile.DateLastModified > maxDate Then
                 maxDate = currentFile.DateLastModified
                 maxFileName = currentFile.Name
            End If

        End If

    Next currentFile

    'MsgBox maxFileName

    Workbooks.Open fso.BuildPath(myFolder, maxFileName) 

End Sub