And here is one more hint for your VBA toolbox! The macro below returns an array of strings with the list of files which are contained in a previously informed folder:
Public Function ListaArquivos(ByVal Caminho As String) As String() 'Atenção: Faça referência à biblioteca Micrsoft Scripting Runtime Dim FSO As New FileSystemObject Dim result() As String Dim Pasta As Folder Dim Arquivo As File Dim Indice As Long ReDim result(0) As String If FSO.FolderExists(Caminho) Then Set Pasta = FSO.GetFolder(Caminho) For Each Arquivo In Pasta.Files Indice = IIf(result(0) = "", 0, Indice + 1) ReDim Preserve result(Indice) As String result(Indice) = Arquivo.Name Next End If ListaArquivos = result ErrHandler: Set FSO = Nothing Set Pasta = Nothing Set Arquivo = Nothing End Function |
The macro below is an example of how to call the “ListFiles” Function created before, listing the files inside C:\Temp folder on your computer:
Private Sub ListaArquivos() Dim arquivos() As String Dim lCtr As Long arquivos = ListaArquivos("C:\temp") For lCtr = 0 To UBound(arquivos) Debug.Print arquivos(lCtr) Next End Sub |
Important: As mentioned in the code, you need to add the reference to the Microsoft Scripting Runtime lib to be able to access the File System Objecst (FSO) objecjs.
Enfoy!