How can I add a folder picker option to this code?

Sheesha1984

New Member
Joined
Feb 15, 2022
Messages
4
Office Version
  1. 2016
Platform
  1. Windows
Hi, I'll preface by saying I didn't create this code so I'm open to any changes. The code is meant to create a list of files in a folder. It gives me a list of files with the extension on column A. I would like to be able to choose what folder to use each time instead of having to replace the code or work out of one particular folder. I know basically nothing about excel macros/VBA but I'm trying to learn. Can someone tell me how to replace the static folder location and create a prompt to choose. Thanks!

Sub LoopThroughFiles()

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("C:\VBA testing")

For Each oFile In oFolder.Files

Cells(i + 1, 1) = oFile.Name

i = i + 1

Next oFile

End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Try this macro

VBA Code:
Sub jec()
 Dim ar(), it As Variant, x As Long
 With Application.FileDialog(4)
   If .Show Then
     For Each it In CreateObject("scripting.filesystemobject").getfolder(.SelectedItems(1)).Files
       ReDim Preserve ar(x)
       ar(x) = it.Name
       x = x + 1
     Next
   End If
 End With
 Sheets(1).Cells(1, 1).Resize(x) = Application.Transpose(ar)
End Sub
 
Upvote 0
Solution
Sub jec() Dim ar(), it As Variant, x As Long With Application.FileDialog(4) If .Show Then For Each it In CreateObject("scripting.filesystemobject").getfolder(.SelectedItems(1)).Files ReDim Preserve ar(x) ar(x) = it.Name x = x + 1 Next End If End With Sheets(1).Cells(1, 1).Resize(x) = Application.Transpose(ar) End Sub
Thank you! That worked great. I appreciate the help.
 
Upvote 0

Forum statistics

Threads
1,214,819
Messages
6,121,749
Members
449,050
Latest member
excelknuckles

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top