【发布时间】:2018-03-09 21:46:53
【问题描述】:
我需要在一个文件夹中获取特定文件类型的所有文件名,并将每个文件名放入一个单独的单元格中,最好按行向下。我还需要确保没有任何重复的文件名,并且这适用于任何文件夹,因为它应该是动态的。
基本上,我需要做类似的事情,但循环,我需要检查以确保它不在工作簿中: VBA Get File Name From Path and Store it to a Cell
我在这里尝试过使用 Application.GetOpenFilename、Dir() 函数和其他一些东西:
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
感谢任何帮助,谢谢!
在我使用其他人的代码之前,我的代码非常简单(编程新手):
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
【问题讨论】:
-
所以你已经尝试过......并且?你有一些你想使用的代码吗?您可以将相关代码以及任何错误或意外行为发布到此帖子吗?到目前为止,您尝试过什么?
-
好吧,我们知道您无论如何都可以复制链接......这不是这个网站的真正运作方式。