Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dir()函数在Mac Excel 2011 VBA中不工作_Excel_Vba_Macos - Fatal编程技术网

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

Dir()函数在Mac Excel 2011 VBA中不工作,excel,vba,macos,Excel,Vba,Macos,您好,我正在尝试列出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 = A

您好,我正在尝试列出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

Gianna,你不能像VBA-EXCEL 2011那样使用
DIR
。我的意思是不支持通配符。为此,您必须使用MACID

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

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

主题:MacID函数

链接

编辑:

如果我怀疑那个链接曾经消失,这里有一个摘录

MacID功能

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

语法

MacID(常数)

必需的常量参数由4个字符组成,用于指定资源类型、文件类型、应用程序签名或Apple事件,例如,文本、OBIN、“XLS5”表示Excel文件(“XLS8”表示Excel 97),Microsoft Word使用“W6BN”(“W8BN”表示Word 97),等等

备注

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

目录(“SomePath”,MacID(“文本”))

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

HTH

如果Dir(outputFileName)“,则
迪曼
ans=MsgBox(“文件已存在。是否继续(将删除上一个文件)?”,vbYesNo)
如果ans=vbNo,则
出口接头
其他的
终止输出文件名
如果结束
如果结束
对于listitem=0到List6.ListCount()-1

对于上面的答案,当我拿出MacID中的“文本”时,它对我起了作用:

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

“无法执行”的确切含义是什么?是否有错误消息?当执行停止时,
strPath
的具体内容是什么?这里需要注意的一个问题是,返回的文件路径被缩短,以适应31个字符的限制(pre-OS X HFS文件系统的文件名限制)。缩短函数保留文件扩展名,但更改其前面的字符,类似于Windows上文件的短路径名。
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
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