Results 1 to 16 of 16

Thread: I need to make a program bullet proof

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #15
    VBAX Expert
    Joined
    Aug 2007
    Location
    Windermere, FL, a 'burb in the greater Orlando metro area.
    Posts
    567
    Location
    Mik, et al,

    I'm trying to read the directory that contains my source and target files, as well as my "program" XL file. I'm using the following development code which works beautifully on my PC and should work on the Mac from what I read in Help.

    As noted below, the line "FileName = Dir(FileSpec)" fails to return anything and I've verified by the debug.print in the calling program that we are in the proper directory/folder. Does Mac expect something different form "*.xls" as the trailing piece of FileSpec ?

    thanks,

    Ron

    Option Explicit
    
    Sub Test_GetFileList()
        Dim p As String, x As Variant, i As Integer
        p = ThisWorkbook.Path & ":*.xls*"
        Debug.Print p
        x = GetFileList(p)
        Select Case IsArray(x)
            Case True 'files found
                MsgBox UBound(x)
                Sheets("Sheet1").Range("A:A").Clear
                For i = LBound(x) To UBound(x)
                    Sheets("Sheet1").Cells(i, 1).Value = x(i)
                Next i
            Case False  'no files found
                MsgBox "No matching files"
        End Select
    End Sub
    
    Function GetFileList(FileSpec As String) As Variant
        '   Returns an array or filename that match FileSpec
        '   If no matching files are found, it returns False
        Dim FileArray() As Variant
        Dim FileCount As Integer
        Dim FileName As String
        FileCount = 0
        FileName = Dir(FileSpec)       '<<< this fails to return a file list
        Debug.Print Dir(FileSpec)
        If FileName = "" Then GoTo NoFilesFound
        '   Loop until no more matching files are found
        Do While FileName <> ""
            FileCount = FileCount + 1
            ReDim Preserve FileArray(1 To FileCount)
            FileArray(FileCount) = FileName
            FileName = Dir()
        Loop
        GetFileList = FileArray
        Exit Function
        NoFilesFound:
        GetFileList = False
    End Function
    Last edited by Aussiebear; 06-22-2025 at 06:55 PM.
    Ron
    Windermere, FL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •