programing

Mac Excel 2011 VBA에서 Dir() 기능이 작동하지 않음

telebox 2023. 9. 4. 19:44
반응형

Mac Excel 2011 VBA에서 Dir() 기능이 작동하지 않음

안녕하세요. 엑셀 워크북이 있는 하위 디렉터리에 있는 모든 파일을 나열하려고 합니다.어떤 이유에서인지 코드는 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

지안나, 당신은 사용할 수 없습니다.DIRVBA-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자 상수를 Dir, Kill, Shell 및 AppActivate에서 사용할 수 있는 값으로 변환하는 데 사용됩니다.

구문

MacID(상수)

필수 상수 인수는 리소스 유형, 파일 유형, 응용 프로그램 서명 또는 Apple 이벤트를 지정하는 데 사용되는 4자로 구성됩니다. 예를 들어, Excel 파일의 경우 TEXT, OBIN, "XLS5"(Excel 97의 경우 XLS8), Microsoft Word는 "W6BN"(Word 97의 경우 W8BN) 등이 있습니다.

언급

MacID는 Dir 및 Kill과 함께 Macintosh 파일 형식을 지정하는 데 사용됩니다.Macintosh는 * 및 ?를 와일드카드로 지원하지 않으므로 대신 4자 상수를 사용하여 파일 그룹을 식별할 수 있습니다.예를 들어 다음 문은 현재 폴더에서 TEXT 유형 파일을 반환합니다.

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

MacID는 Shell 및 AppActivate와 함께 응용 프로그램의 고유한 서명을 사용하여 응용 프로그램을 지정하는 데 사용됩니다.

HTH

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

위의 답변은 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

언급URL : https://stackoverflow.com/questions/10045474/dir-function-not-working-in-mac-excel-2011-vba

반응형