Dir()函数在Mac Excel 2011 VBA中不起作用

AG1*_*G10 13 excel vba excel-vba excel-vba-mac

您好我正在尝试列出Excel工作簿所在的子目录中的所有文件.由于某种原因,代码无法执行超出Dir函数.任何人都可以建议吗?谢谢!

Sub ListFiles()

    ActiveSheet.Name = "temp"

    Dim MyDir As String
    'Declare the variables
    Dim strPath As String
    Dim strFile As String
    Dim r As Long

    MyDir = ActiveWorkbook.Path 'current path where workbook is
    strPath = MyDir & ":Current:" 'files within "Current" folder subdir, I am using Mac Excel 2011

    'Insert the headers in Columns A, B, and C
    Cells(1, "A").Value = "FileName"
    Cells(1, "B").Value = "Size"
    Cells(1, "C").Value = "Date/Time"

    'Find the next available row
    r = Cells(Rows.Count, "A").End(xlUp).Row + 1

    'Get the first file from the folder
            'Note: macro stops working here
    strFile = Dir(strPath & "*.csv", vbNormal)

    'Loop through each file in the folder
    Do While Len(strFile) > 0

        'List the name, size, and date/time of the current file
        Cells(r, 1).Value = strFile
        Cells(r, 2).Value = FileLen(strPath & strFile)
        Cells(r, 3).Value = FileDateTime(strPath & strFile)

        'Determine the next row
        r = r + 1

        'Get the next file from the folder
        strFile = Dir

    Loop

    'Change the width of the columns to achieve the best fit
    Columns.AutoFit

End Sub
Run Code Online (Sandbox Code Playgroud)

Sid*_*out 19

Gianna,你不能DIR在VBA-EXCEL 2011中使用它.我的意思是不支持通配符.您必须使用MACID才能实现此目的.

请参阅此代码示例(经过测试和测试)

Sub Sample()
    MyDir = ActiveWorkbook.Path
    strPath = MyDir & ":"

    strFile = Dir(strPath, MacID("TEXT"))

    'Loop through each file in the folder
    Do While Len(strFile) > 0
        If Right(strFile, 3) = "csv" Then
            Debug.Print strFile
        End If

        strFile = Dir    
    Loop
End Sub
Run Code Online (Sandbox Code Playgroud)

有关MACID的更多详细信息,请参阅此链接

主题:MacID功能

链接:http://office.microsoft.com/en-us/access-help/macid-function-HA001228879.aspx

编辑:

如果链接永远死亡我怀疑,这是一个提取.

MacID功能

在Macintosh上用于将4个字符的常量转换为Dir,Kill,Shell和AppActivate可能使用的值.

句法

MACID(常量)

所需的常量参数由4个字符组成,用于指定资源类型,文件类型,应用程序签名或Apple事件,例如TEXT,OBIN,Excel文件的"XLS5"(Excel 97的"XLS8"),Microsoft Word使用"W6BN"(Word 97的"W8BN"),依此类推.

备注

MacID与Dir和Kill一起使用以指定Macintosh文件类型.由于Macintosh不支持*和?作为通配符,您可以使用四字符常量来标识文件组.例如,以下语句从当前文件夹返回TEXT类型文件:

Dir("SomePath",MacID("TEXT"))

Mac与Shell和AppActivate一起使用,以使用应用程序的唯一签名指定应用程序.

HTH