I need to grab all the filenames of a particular filetype in a folder, and put each filename into a separate cell, preferably going down by rows. I also need to make sure that there aren't any duplicate filenames and that this will work for any folder, as it's supposed to be dynamic.
Basically, I need to do something similar to this, but looped and I need to put in a check to make sure it's not already in the workbook: VBA Get File Name From Path and Store it to a Cell
I've tried using Application.GetOpenFilename, Dir() function, and some other stuff here:
count files in specific folder and display the number into 1 cel
Using Excel VBA to loop through .csv files in folder and copy the filename into cells in last column
Any help is appreciated, thanks!
the code I had is pretty barebones (new to programming) before I used other's code:
Sub Add_Policies()
'let user select folder, go into folder, grab all filenames which end in .htm, put each into a separate cell, one after the other.
'This needs to be dynamic, so probably put in an Update List button. Msgbox "x number of policies were added. There are now a total of y policies."
'check if policy is already present. if so, skip.
'add functionality to open a policy in excel
Dim fldr As FileDialog, nFiles As Integer, fldrName As String, FileDifference As Integer, FileName As String
'Open the select folder prompt
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = "C:\Users\Hothi\Documents"
If .Show = -1 Then 'if ok is pressed
fldrName = .SelectedItems(1)
End If
End With
If fldrName <> "" Then
With fldrName
path = fldrName & "\*.htm"
FileName = Dir("path")
Do While FileName <> ""
nFiles = nFiles + 1
FileName = Dir()
Loop
For i = 1 To nFiles
Range("A3").Offset(i, 0) = nFiles
Range.Value = Dir(
Next
If nFiles <> nFiles Then
msgbox (" & FileDifference & number of policies added. There are now a total of & nFiles & policies.")
Else: msgbox ("No new policies, check location of new policies.")
End If
End Sub