列出文件夹中的文件

时间:2016-07-28 22:58:23

标签: excel macos vba excel-vba macros

我使用以下代码让用户选择一个文件夹,然后列出上次修改每个文件的时间(一天为一列,另一列为时间)。第三列是文件名。

Sub ListFils()
Dim f As Object, fso As Object, flder As Object
Dim folder As String
Dim wb As Workbook, ws As Worksheet

Set wb = ActiveWorkbook
Set ws = ActiveSheet
Set fso = CreateObject("Scripting.FileSystemObject")

With Application.FileDialog(msoFileDialogFolderPicker)
    .Show
    If .SelectedItems.Count = 0 Then
        MsgBox "Cancel Selected"
        End
    End If
    folder = .SelectedItems(1)
End With

Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents


For Each f In fso.GetFolder(folder).Files
    ws.Range("A" & ws.Rows.Count).End(xlUp).Offset(1, 0) = f.DateLastModified
    ws.Range("B" & ws.Rows.Count).End(xlUp).Offset(1, 0) = f.DateLastModified
    ws.Range("C" & ws.Rows.Count).End(xlUp).Offset(1, 0) = f.Name


Next
Columns("A:C").Columns.AutoFit
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
End Sub

该代码适用于Windows,但不适用于Mac。我有什么想法可以让它发挥作用吗?

1 个答案:

答案 0 :(得分:0)

正如蒂姆在评论中所说,行Set fso = CreateObject("Scripting.FileSystemObject")以及任何依赖fso的行都无法在mac上运行,但您可以使用Dir()来获取文件名,{ {1}}获取修改日期。