|
楼主 |
发表于 2019-4-18 17:04
|
显示全部楼层
本帖最后由 盘尼西林99 于 2019-4-18 18:38 编辑
Sub GetAllFiles()
Rem 下载自http://club.excelhome.net/thread-1391369-3-1.html
Dim pth$, arr
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show = -1 Then
pth = .SelectedItems(1)
Else
MsgBox "您没有选择任何文件夹!", vbCritical: Exit Sub
End If
End With
ReDim arr(1 To 6, 1 To 1)
arr(1, 1) = "文件名称"
arr(2, 1) = "文件位置"
arr(3, 1) = "创建日期"
arr(4, 1) = "修改日期"
arr(5, 1) = "文件类型"
arr(6, 1) = "文件大小"
Getfd pth, arr
arr = Application.WorksheetFunction.Transpose(arr) '文件信息已保存在arr数组中
'实际使用时可不输出到工作表,直接在数组arr中查询需要的文件信息。以下10行可删除
Application.ScreenUpdating = False
With ActiveSheet
.UsedRange.Clear
.Cells(1, 1).Resize(UBound(arr, 1), UBound(arr, 2)) = arr
r = .Range("a" & Rows.Count).End(3).Row
Rem 添加链接 2019-4-17 15时测试,在B列可以添加超链接,并省略后面的代码
For Each c In .Range("b2:b" & r)
' .Hyperlinks.Add Anchor:=c, Address:=c.Value, TextToDisplay _
' :=Split(c, "\")(UBound(Split(c, "\")))
.Hyperlinks.Add Anchor:=c, Address:=c.Value
rem 请大师修改上面的代码。本人试了一下,如将For Each c In .Range("b2:b" & r)修改为For Each c In .Range("A2:A" & r),貌似加上了超链接,但无法打开文件。
Next
.Range("a1:f" & r).Borders.LineStyle = xlContinuous
.Range("a1:f" & r).Borders.Weight = xlThin
End With
Application.ScreenUpdating = True
MsgBox "文件已全部获取!点『确定』键结束"
End Sub
Sub Getfd(ByVal pth As String, arr)
Dim fso As Object, f, fd, ff
Set fso = CreateObject("scripting.filesystemobject")
Set ff = fso.getfolder(pth)
For Each f In ff.Files
ReDim Preserve arr(1 To 6, 1 To UBound(arr, 2) + 1)
u = UBound(arr, 2)
arr(1, u) = f.Name
arr(2, u) = f
arr(3, u) = f.DateCreated
arr(4, u) = f.DateLastModified
' arr(5, u) = f.Type
Rem 将文件类型修改为文件后缀名 2019-4-15 19:35
arr(5, u) = Mid(f.Name, InStrRev(f.Name, ".", -1, 1) + 1)
arr(6, u) = Format(f.Size / 1048576, "0.00MB")
Next
For Each fd In ff.subfolders: Getfd fd, arr: Next
End Sub
|
|