ばくがの業務効率化チャンネル

このブログは個人的なエクセルの備忘録です

フォルダからファイルを取得

Sub Sample3()

    Dim f As Object, cnt As Long
    
    
    cnt = 1
    
    'A列に作成日時
    Cells(1, 1) = "作成日時"
    
    'B列にファイル名称
    Cells(1, 2) = "ファイル名"
    
    'C列にファイルサイズ
    Cells(1, 3) = "ファイルサイズ"
    
    With CreateObject("Scripting.FileSystemObject")
        
        For Each f In .GetFolder("C:\Users\Desktop\テスト").Files
            
            '2行目以降に、フォルダ内のファイルの取得結果を記載する
            cnt = cnt + 1
            
            Cells(cnt, 1) = f.DateCreated
            
            Cells(cnt, 2) = f.Name
            
            Cells(cnt, 3) = f.Size
            
        
        Next f
    
    End With

End Sub