Skip to content

Instantly share code, notes, and snippets.

@deyvicode
Created September 7, 2022 22:01
Show Gist options
  • Save deyvicode/dc4a5e948d5fb60b4cb0419dca6b75df to your computer and use it in GitHub Desktop.
Save deyvicode/dc4a5e948d5fb60b4cb0419dca6b75df to your computer and use it in GitHub Desktop.
VBA read files in folder
Sub LoopThroughFiles()
Debug.Print ""
Debug.Print "-------------------------------------------------"
Dim oFSO As Object
Dim oFolder As Object
Dim oFile As Object
Dim i As Integer
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(GetFolderPath)
For Each oFile In oFolder.Files
'Cells(i + 1, 1) = oFile.Name
'Cells(i + 1, 2) = oFile.Path
Debug.Print oFile.Name & " => " & oFile.Path
i = i + 1
Next oFile
End Sub
Function GetFolderPath() As String
Dim fldr As FileDialog
Dim sItem As String
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Select a Folder"
.AllowMultiSelect = False
.InitialFileName = Application.DefaultFilePath
If .Show <> -1 Then GoTo NextCode
sItem = .SelectedItems(1)
End With
NextCode:
GetFolderPath = sItem
Set fldr = Nothing
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment