Created
September 7, 2022 22:01
-
-
Save deyvicode/dc4a5e948d5fb60b4cb0419dca6b75df to your computer and use it in GitHub Desktop.
VBA read files in folder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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