Get list of Excel files in a folder using VBA

Buzz Lightyear

I need to get the names of all the Excel files in a folder and then make changes to each file. I've gotten the "make changes" part sorted out. Is there a way to get a list of the .xlsx files in one folder, say D:\Personal and store it in a String Array.

I then need to iterate through the list of files and run a macro on each of the files which I figured I can do using:

Filepath = "D:\Personal\"
For Each i in FileArray
    Workbooks.Open(Filepath+i)
Next

I had a look at this, however, I wasn't able to open the files cause it stored the names in Variant format.

In short, how can I use VBA to get a list of Excel filenames in a specific folder?

Coder375

Ok well this might work for you, a function that takes a path and returns an array of file names in the folder. You could use an if statement to get just the excel files when looping through the array.

Function listfiles(ByVal sPath As String)

    Dim vaArray     As Variant
    Dim i           As Integer
    Dim oFile       As Object
    Dim oFSO        As Object
    Dim oFolder     As Object
    Dim oFiles      As Object

    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set oFolder = oFSO.GetFolder(sPath)
    Set oFiles = oFolder.Files

    If oFiles.Count = 0 Then Exit Function

    ReDim vaArray(1 To oFiles.Count)
    i = 1
    For Each oFile In oFiles
        vaArray(i) = oFile.Name
        i = i + 1
    Next

    listfiles = vaArray

End Function

It would be nice if we could just access the files in the files object by index number but that seems to be broken in VBA for whatever reason (bug?).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Trying to get this VBA in Excel to open a specific folder to select excel files to merge, I seem to be going round in circles as usual

Using maven replacer plugin to list files in a folder

Loop through files in a folder using VBA?

How to get list files from a github repository folder using R

List files in iTunes shared folder using Swift

Get an ordered list of files in a folder

Excel VBA updating a master list from files in folder

VBA For Excel (CSV), Looping through files to get row, then appending rows into one List

get list of files in a folder BUT in a predetermined order

Get folder by regex in `list.files()`?

Rename Files Using List in Excel

Get list of files in a specific Netsuite folder

How to get a list of a cell's font values in Excel using VBA

Copying Files To a Dynamic Folder Using VBA

EXCEL VBA get source folder to Const

How can I copy a folder with files to a FTP folder using VBA?

Delete non-Excel Files in a folder VBA

Get list of file names in folder/directory with Excel VBA

remove files then folder in excel 2010 vba

How to open a folder from a list in an Excel spreadsheet using VBA

how to list files in a folder using bash scripting

Excel VBA code to bulk convert rtf files in a folder to pdf files

Printing all Excel files in a folder using PowerShell

merging all pdf files in a folder using excel vba

List files from certain folder using batch

VBA loop through excel files in folder

How to list files under a folder using jenkins

VBA - Partially renaming Excel files in a folder

How to move a specific list of excel files to another folder using VBA