我正在使用此代码列出Excel中的文件夹和子文件夹中的所有文件。此代码工作正常。我想为每个子文件夹留下一个空行。目前它的列表在所有行中连续出现。请帮忙。目录中的文件夹子文件夹中的文件列表
Sub HyperlinkDirectory()
Dim fPath As String
Dim fType As String
Dim fname As String
Dim NR As Long
Dim AddLinks As Boolean
'Select folder
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
.InitialFileName = "C:\2009\"
.Show
If .SelectedItems.Count > 0 Then
fPath = .SelectedItems(1) & "\"
Else
Exit Sub
End If
End With
'Types of files
fType = Application.InputBox("What kind of files? Type the file extension to collect" _
& vbLf & vbLf & "(Example: pdf, doc, txt, xls, *)", "File Type", "pdf", Type:=2)
If fType = "False" Then Exit Sub
'Option to create hyperlinks
AddLinks = MsgBox("Add hyperlinks to the file listing?", vbYesNo) = vbYes
'Create report
Application.ScreenUpdating = False
NR = 5
With Sheets("Sheet1")
.Range("A:C").Clear
.[A1] = "Directory"
.[B1] = fPath
.[A2] = "File type"
.[B2] = fType
.[A4] = "File"
.[B4] = "Modified"
Call FindFilesAndAddLinks(fPath, fType, NR, AddLinks)
.Range("A:B").Columns.AutoFit
End With
Application.ScreenUpdating = True
End Sub
Private Sub FindFilesAndAddLinks(fPath As String, fType As String, ByRef NR As Long, AddLinks As Boolean)
Dim fname As String
Dim oFS As New FileSystemObject
Dim oDir
'Files under current dir
fname = Dir(fPath & "*." & fType)
With Sheets("Sheet1")
Do While Len(fname) > 0
'filename
.Range("A" & NR) = fname
'modified
.Range("B" & NR) = FileDateTime(fPath & fname)
'hyperlink
.Range("A" & NR).Select
If AddLinks Then .Hyperlinks.Add Anchor:=Selection, _
Address:=fPath & fname, _
TextToDisplay:=fPath & fname
'set for next entry
NR = NR + 1
fname = Dir
Loop
'Files under sub dir
Set oDir = oFS.GetFolder(fPath)
For Each oSub In oDir.SubFolders
Call FindFilesAndAddLinks(oSub.Path & "\", fType, NR, AddLinks)
Next oSub
End With
End Sub
CLR非常感谢你,你的代码工作正常,但我正在'目前目录下的文件 'fname = Dir(fPath&“*。”&fType) “和”对于oDir.SubFolders中的每个oSub“ –
您是否正在使用添加文件夹名称的最新版本? – CLR
你的新代码工作正常。现在它显示的文件夹名称非常感谢你............. –