0

複雑な問題があります。説明するのはもっと複雑ですが、始めさせてください。

Excel セルにパスがあり (パスが共有につながる)、VBA で実行したいと考えています。

最終変更日が最も短いファイルを取得したいのですが (ファイルはそのフォルダーで最後に変更されました)、スキャンしているフォルダーに含まれるサブフォルダーの数がわかりません。

私の質問は、ファイルの最終更新日をフォルダーでスキャンする方法と、このフォルダーのサブフォルダー (サブフォルダーがある場合) をスキャンする方法です。

4

1 に答える 1

0

このコードを試してください:

Option Explicit

Public newestFile As Object

Sub start()
  Call getNewestFile("C:\yourpath")
  MsgBox newestFile.Name
End Sub

Private Sub getNewestFile(folderPath As String)
    Dim objFSO As Object
    Dim objFolder As Object
    Dim objFile As Object

    'get the filesystem object from the system
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.GetFolder(folderPath)


  'go through the subfolder and call itself
  For Each objFile In objFolder.SubFolders
    Call getNewestFile(objFile.Path)
  Next

  For Each objFile In objFolder.Files
    If newestFile Is Nothing Then
      Set newestFile = objFile
    ElseIf objFile.DateLastModified > newestFile.DateLastModified Then
      Set newestFile = objFile
    End If
  Next
End Sub
于 2013-10-17T13:53:55.307 に答える