【问题标题】:Open most recent .csv file in a folder在文件夹中打开最新的 .csv 文件
【发布时间】:2018-02-28 19:29:20
【问题描述】:

我对 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

【问题讨论】:

  • QHarr 是的,最后修改的文件,只要是 .csv 文件就可以了

标签: vba


【解决方案1】:

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

然后添加对MS Scripting runtime的引用(工具>引用)

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

【讨论】:

  • 编译错误“user-defined type not defined”是什么意思?
  • 我在运行代码时仍然收到未找到路径的错误。我也不确定我在哪里告诉代码从我的 Z: 驱动器中提取文件。对不起,我慢了,我还在学习。感谢您抽出宝贵时间帮助 QHarr!
  • 这不是问题。所以我们已经清除了最初的错误。现在,您应该已将此 Const folderPath As String = "C:\Users\User\Desktop\Test" 替换为 Const folderPath As String = "yourfolderpath\yourfolder" 以访问包含您正在使用的 CSV 的文件夹。顺便说一句,您是否使用 Windows 机器,因为 mac 的过程略有不同。不要害怕提问,尽管明天就是回复时间。
  • 谢谢。慢慢来。我将“yourfolderpath\yourfolder”替换为“Z:/”此时代码运行正常,但唯一发生的情况是弹出一个空消息框。
  • 不,我在 PC 上使用 Windows。但我只是从文件资源管理器目标栏中复制文件夹的目标。它给了我 Z:\
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-30
  • 2021-10-17
  • 1970-01-01
  • 1970-01-01
  • 2021-10-14
  • 1970-01-01
相关资源
最近更新 更多