|
回复 24楼 yf_992258 的帖子
ub 建立资料目录'使用双字典,旨在提高速度
Dim MyName, Dic, Did, I, T, F, TT, MyFileName, objShell, objFolder, lj, Ke, sz, Sh, rng As Range, cell As Range
'On Error Resume Next
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder(0, "选择文件夹", 0, 0)
If Not objFolder Is Nothing Then lj = objFolder.self.Path & "\"
Set objFolder = Nothing
Set objShell = Nothing
T = Timer
Set Dic = CreateObject("Scripting.Dictionary") '创建一个字典对象
Set Did = CreateObject("Scripting.Dictionary")
Dic.Add (lj), ""
I = 0
Do While I < Dic.Count
Ke = Dic.keys '开始遍历字典
MyName = Dir(Ke(I), vbDirectory) '查找目录
Do While MyName <> ""
If MyName <> "." And MyName <> ".." Then
If (GetAttr(Ke(I) & MyName) And vbDirectory) = vbDirectory Then '如果是次级目录
Dic.Add (Ke(I) & MyName & "\"), "" '就往字典中添加这个次级目录名作为一个条目
End If
End If
MyName = Dir '继续遍历寻找
Loop
I = I + 1
Loop
sz = Split(lj, "\")
Did.Add (sz(UBound(sz) - 1) & "文件清单"), "" '以查找D盘下所有EXCEL文件为例
For Each Ke In Dic.keys
MyFileName = Dir(Ke & "*.*")
Do While MyFileName <> ""
Did.Add (Ke & MyFileName), ""
MyFileName = Dir
Loop
Next
For Each Sh In ThisWorkbook.Worksheets
If Sh.Name = sz(UBound(sz) - 1) & "文件清单" Then
Sheets(sz(UBound(sz) - 1) & "文件清单").Cells.Delete
F = True
Exit For
Else
F = False
End If
Next
If Not F Then
Sheets.Add.Name = sz(UBound(sz) - 1) & "文件清单"
End If
Sheets(sz(UBound(sz) - 1) & "文件清单").[A1].Resize(Did.Count, 1) = WorksheetFunction.Transpose(Did.keys)
j = Did.Count
Set rng = Range("a2:A" & j)
For S = 2 To j
For Each cell In rng
Cells(S, 1).Hyperlinks.Add Anchor:=cell, Address:=lj
Next cell
rng.EntireRow.AutoFit
Next S
Set rng = Nothing
TT = Timer - T
MsgBox TT 'Minute(TT) & "分" & Second(TT) & "秒"
End Sub
再请教大灰狼大师帮看看,我这段建立目录与原文件链接的代码(红色字体部分)问题在哪里.点击目录时,首先出现的是"MS OFFICE 安全声明"对话框,点确定后,只能链接到打开一级文件夹.又麻烦你了. |
|