-->

迪尔()函数没有在Mac的Excel 2011 VBA工作迪尔()函数没有在Mac的Excel 20

2019-05-13 11:17发布

你好我想列出在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

Answer 1:

吉安娜,你不能使用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

请参阅此链接上MACID更多详情

主题:MACID功能

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

编辑:

如果该链接会消逝,我怀疑,这里是一个提取物。

MACID功能

使用在Macintosh到一个4个字符的常数转换为可以通过风向,杀死,壳牌和则AppActivate被使用的值。

句法

MACID(常量)

所需的常量参数包括用于指定资源类型4个字符,文件类型,应用程序签名,或苹果事件,例如文本,OBIN,“XLS5”为Excel文件(“XLS8”为Excel 97中),微软Word使用“W6BN”( “W8BN” 用于Word 97),等等。

备注

MACID用于与导演和杀死指定Macintosh文件类型。 由于Macintosh不支持*和? 作为通配符,你可以使用一个四字符常量,而不是来识别文件组。 例如,下面的语句从当前文件夹中返回TEXT类型的文件:

迪尔( “SomePath”,MACID( “TEXT”))

MACID使用与壳牌和使用AppActivate指定使用应用程序的唯一签名的应用程序。

HTH



Answer 2:

If Dir(outputFileName) <> "" Then
Dim ans
ans = MsgBox("File already exists.Do you wish to continue(the previous file will be    deleted)?", vbYesNo)
If ans = vbNo Then
Exit Sub
Else
Kill outputFileName
End If
End If

For listitem = 0 To List6.ListCount() - 1


Answer 3:

对于上面的答案,它为我工作,当我在MACID拿出“TEXT”:

Sub LoopThruFiles()

    Dim mydir As String
    Dim foldercount As Integer
    Dim Subjectnum As String
    Dim strpath As String
    Dim strfile As String

    ChDir "HD:Main Folder:"
    mydir = "HD:Main Folder:"
    SecondaryFolder = "Folder 01:"
    strpath = mydir & SecondaryFolder

    strfile = Dir(strpath)

    'Loop through each file in the folder
    Do While Len(strfile) > 0
     If Right(strfile, 3) = "cef" Then
        MsgBox (strfile)
        End If
        strfile = Dir
    Loop
End Sub


文章来源: Dir() function not working in Mac Excel 2011 VBA