I'm trying to write some code that will create a dropdown list that contains all files with a certain file extension in a folder. Initial code here:
Dim FSOLibrary As Object
Dim FSOFolder As Object
Dim FSOFile As Object
Dim fp As String
Dim i As Integer
fp = Environ("UserProfile") & "\OneDrive\Desktop\Test"
Set FSOLibrary = CreateObject("Scripting.FileSystemObject")
Set FSOFolder = FSOLibrary.GetFolder(fp)
Set FSOFile = FSOFolder.Files
i = 1
For Each FSOFile In FSOFile
If FSOFile Like "*.txt*" Then
'just put the name into column B for testing
Range("B" & i).Value = FSOFile.Name
i = i + 1
End If
Next FSOFile
Obviously I need to add the data validation part of the code in, but I'm not sure how best to construct it. The number of files is dynamic.
I was thinking of putting all the files that match the desired file extension type into an array, and then writing each entry of the array into the data validation section?
I've seen the Dir() used quite a lot but I don't fully understand it, so have opted to use fso.
