List Files

Jaye7

Well-known Member
Joined
Jul 7, 2010
Messages
1,069
I found the following code on this forum, however can someone please modify a couple of things for me.

I would like the script to list the full file name and path i.e C:\Test\Help.xls, also I would like it to ask what folder you want to list the files from (i.e. popup box to select folder) and also a popup which asks you to specify file type, i.e. .jpg, .xls, .wma etc...


Code:
'Written: February 19, 2009 (Updated April 03, 2009)
'Author:  Leith Ross
'Summary: Creates a file list and copies it to the Active Sheet start at "A1".
'         The arguments are the directory to be searched, the file type (extension),
'         and optionally a wildcard filter for file names. Only the rquired argument
'         is the directory path.
Function CreateFileList(ByVal FolderPath As String, Optional ByVal FileType As String, Optional ByVal FileFilter As String) As Variant
  Dim Cnt As Long
  Dim FileList() As String
  Dim FileName As String
    On Error GoTo OutOfHere
 
    If Right(FolderPath, 1) <> "\" Then FolderPath = FolderPath & "\"
    If FileType = "" Then
       FileType = ".*"
    Else
       If Left(FileType, 1) <> "." Then FileType = "." & FileType
    End If
    If FileFilter = "" Then FileFilter = "*"
 
      FileName = Dir(FolderPath & FileFilter & FileType)
        Do While FileName <> ""
          Cnt = Cnt + 1
          ReDim Preserve FileList(Cnt)
          FileList(Cnt) = FileName
          FileName = Dir()
       Loop
 
OutOfHere:
     If Err = 0 Then CreateFileList = FileList
     On Error GoTo 0
 
End Function
Sub ListFiles()
  Dim I As Long
  Dim MyFiles As Variant
  Dim MyArray() As String
  Dim N As Long
  Dim Rng As Range
  Dim RngEnd As Range
 
    MyFiles = CreateFileList("C:\Test\", ".xls")
 
      N = UBound(MyFiles)
      ReDim MyArray(N, 0)
 
        For I = 1 To N
          MyArray(I, 0) = MyFiles(I)
        Next I
 
        Set Rng = Worksheets("Sheet2").Range("A2")
        Set Rng = Cells(Rows.Count, Rng.Column).End(xlUp)
        Set Rng = Rng.Resize(N + 1, 1)
 
        Rng = MyArray
 
End Sub

Thanks


.
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Code:
Sub ListFiles()
  Dim I As Long
  Dim MyFiles As Variant
  Dim MyArray() As String
  Dim N As Long
  Dim Rng As Range
  Dim RngEnd As Range
[COLOR="Blue"]  Dim strPath As String
  Dim Ext As String
  
    With Application.FileDialog(msoFileDialogFilePicker)
        .Title = "Select the folder and one file of the type you want listed"
        .InitialFileName = "*.*"
        .Show
        If .SelectedItems.Count Then
            strPath = .SelectedItems(1)
        Else: Exit Sub  ' user canceled
        End If
    End With
    
    Ext = Mid(strPath, InStrRev(strPath, "."))
    strPath = Left(strPath, InStrRev(strPath, Application.PathSeparator))[/COLOR]
    
    MyFiles = CreateFileList([COLOR="Blue"]strPath, Ext[/COLOR])
 
      N = UBound(MyFiles)
      ReDim MyArray(N, 0)
 
        For I = 1 To N
          MyArray(I, 0) = [COLOR="Blue"]strPath &[/COLOR] MyFiles(I)
        Next I
 
        Set Rng = Worksheets("Sheet2").Range("A2")
        Set Rng = Cells(Rows.Count, Rng.Column).End(xlUp)
        Set Rng = Rng.Resize(N + 1, 1)
 
        Rng = MyArray
 
End Sub
 
Upvote 0
Thanks for the script but I get a compile error on the following line saying sub or function not defined.


Code:
MyFiles = CreateFileList(strPath, Ext)
 
Upvote 0
Did you delete the Function CreateFileList? It was in your original code that you posted. You still need that part of the code.

The new code I posted only replaces the procedure Sub ListFiles() on down.
 
Upvote 0
Sorry, I just copied your script to a separate module and tried it.

That's Perfect, works great, thanks Alphafrog.
 
Upvote 0

Forum statistics

Threads
1,224,613
Messages
6,179,904
Members
452,948
Latest member
Dupuhini

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top