{"id":99,"date":"2011-01-13T14:27:17","date_gmt":"2011-01-13T13:27:17","guid":{"rendered":"http:\/\/mariaevert.dk\/vba\/?p=99"},"modified":"2011-01-13T14:34:53","modified_gmt":"2011-01-13T13:34:53","slug":"vba-looping-through-all-files-in-a-folder","status":"publish","type":"post","link":"https:\/\/mariaevert.dk\/vba\/?p=99","title":{"rendered":"VBA &#8211; Looping through all files in a folder"},"content":{"rendered":"<p>This posts looks a lot like my previous &#8211; but it&#8217;s a bit simpler. Here I just show how to loop through files in a specific folder, which the user has chosen in a modal window. <\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nSub ListFiles()\r\n\r\nDim fd As FileDialog\r\nDim PathOfSelectedFolder As String\r\nDim SelectedFolder\r\nDim SelectedFolderTemp\r\nDim MyPath As FileDialog\r\nDim fs\r\nDim ExtraSlash\r\nExtraSlash = &quot;\\&quot;\r\nDim MyFile\r\n\r\n'Prepare to open a modal window, where a folder is selected\r\nSet MyPath = Application.FileDialog(msoFileDialogFolderPicker)\r\nWith MyPath\r\n'Open modal window\r\n        .AllowMultiSelect = False\r\n        If .Show Then\r\n            'The user has selected a folder\r\n            \r\n            'Loop through the chosen folder\r\n            For Each SelectedFolder In .SelectedItems\r\n\r\n                'Name of the selected folder\r\n                PathOfSelectedFolder = SelectedFolder &amp; ExtraSlash\r\n               \r\n                Set fs = CreateObject(&quot;Scripting.FileSystemObject&quot;)\r\n                Set SelectedFolderTemp = fs.GetFolder(PathOfSelectedFolder)\r\n                    \r\n                    'Loop through the files in the selected folder\r\n                    For Each MyFile In SelectedFolderTemp.Files\r\n                        'Name of file\r\n                        MsgBox MyFile.Name\r\n                        'DO STUFF TO THE FILE, for example:\r\n                        'Open each file: \r\n                        'Workbooks.Open FileName:=MyFile\r\n                        \r\n                    Next\r\n\r\n               \r\n            Next\r\n        End If\r\nEnd With\r\n\r\nEnd Sub\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This posts looks a lot like my previous &#8211; but it&#8217;s a bit simpler. Here I just show how to loop through files in a specific folder, which the user has chosen in a modal window.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[],"_links":{"self":[{"href":"https:\/\/mariaevert.dk\/vba\/index.php?rest_route=\/wp\/v2\/posts\/99"}],"collection":[{"href":"https:\/\/mariaevert.dk\/vba\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mariaevert.dk\/vba\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mariaevert.dk\/vba\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mariaevert.dk\/vba\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=99"}],"version-history":[{"count":4,"href":"https:\/\/mariaevert.dk\/vba\/index.php?rest_route=\/wp\/v2\/posts\/99\/revisions"}],"predecessor-version":[{"id":104,"href":"https:\/\/mariaevert.dk\/vba\/index.php?rest_route=\/wp\/v2\/posts\/99\/revisions\/104"}],"wp:attachment":[{"href":"https:\/\/mariaevert.dk\/vba\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=99"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mariaevert.dk\/vba\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=99"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mariaevert.dk\/vba\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=99"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}