Usuários do Excel
Gostaria de reagir a esta mensagem? Crie uma conta em poucos cliques ou inicie sessão para continuar.

listar os arquivos em um diretório

Ir para baixo

listar os arquivos em um diretório Empty listar os arquivos em um diretório

Mensagem por alexandrevba Qua Out 30, 2013 8:39 am

Bom dia!!

Veja como listar os arquivos em um diretório para a coluna A de uma Guia.
Código:
Sub ListarNOmeDeArquivos()
 'Autor Sean
 'Localidade: HerneBay, Kent, UK
 'Feito em:30/10/2013
 Dim fd As FileDialog
 Dim PathOfSelectedFolder As String
 Dim SelectedFolder
 Dim SelectedFolderTemp
 Dim MyPath As FileDialog
 Dim fs
 Dim ExtraSlash
 ExtraSlash = "\"
 Dim MyFile
 Dim x As Integer
 x = 0

 'Prepare to open a modal window, where a folder is selected
 Set MyPath = Application.FileDialog(msoFileDialogFolderPicker)
 With MyPath
 'Open modal window
        .AllowMultiSelect = False
        If .Show Then
            'The user has selected a folder

            'Loop through the chosen folder
            For Each SelectedFolder In .SelectedItems

                'Name of the selected folder
                PathOfSelectedFolder = SelectedFolder & ExtraSlash
                Set fs = CreateObject("Scripting.FileSystemObject")
                Set SelectedFolderTemp = fs.GetFolder(PathOfSelectedFolder)
                    'Loop through the files in the selected folder
                    For Each MyFile In SelectedFolderTemp.Files
                        x = x + 1
                        'Name of file
                        Sheet1.Range("A" & x).Value = MyFile.Name
                        'MsgBox MyFile.Name
                        'DO STUFF TO THE FILE, for example:
                        'Open each file:
                        'Workbooks.Open FileName:=MyFile
                    Next
            Next
        End If
 End With
End Sub
alexandrevba
alexandrevba
Intermediário
Intermediário

Mensagens : 222
Data de inscrição : 26/06/2012
Localização : Serra-ES

Ir para o topo Ir para baixo

Ir para o topo

- Tópicos semelhantes

 
Permissões neste sub-fórum
Não podes responder a tópicos