|
无限趋向 发表于 2012-12-11 15:37
Sub Macro1()
Dim Fso As Object, arr, a, b, s$, t, i&
Dim arrf$(), mf&, d As Object - Sub Macro1() '主程序
- Dim Fso As Object, arr, a, b, s$, t, i& '声明变量,下同
- Dim arrf$(), mf&, d As Object
- Application.ScreenUpdating = False '禁止屏幕刷新
- Set d = CreateObject("scripting.dictionary") '创建字典对象
- arr = Range("E1:E" & Range("E65536").End(xlUp).Row) 'E列数据写入数组
- For i = 3 To UBound(arr) '逐行
- d(arr(i, 1)) = i '用字典把E列内容和行号关联起来
- Next
- Set Fso = CreateObject("Scripting.FileSystemObject") '创建Fso对象
- p = ThisWorkbook.Path & "\河北分公司制度目录" '路径
- Call GetFolders(p, Fso, arrf, mf) '调用GetFolders子程序
- With ActiveSheet '当前工作表
- For i = 1 To mf '逐个文件或文件夹
- a = Split(arrf(i), "") '""分开
- s = a(UBound(a)) '最后一个
- a = Split(s, "(") '再用"("分开
- s = a(0) '第一个
- b = Split(s, "-") '再用"-"分开
- s = b(UBound(b)) '最后一个
- b = Split(s, ".") '再用"."分开
- s = b(0) '第一个
- t = d(s) '求出该字符串在E列的行号
- If t <> "" Then .Hyperlinks.Add Anchor:=Cells(t, 10), Address:=arrf(i) '如果这个行号存在,则创建超级链接
- Next
- End With
- Set Fso = Nothing '释放内存
- Application.ScreenUpdating = True '开启屏幕刷新
- End Sub
- Private Sub GetFolders(ByVal sPath$, Fso As Object, ByRef arrf$(), ByRef mf&) '求出子文件夹和子文件的子程序
- Dim Folder As Object
- Dim SubFolder As Object
- Set Folder = Fso.GetFolder(sPath)
- mf = mf + 1
- ReDim Preserve arrf(1 To mf)
- arrf(mf) = sPath '子文件夹写入数组
- For Each File In Folder.Files
- mf = mf + 1
- ReDim Preserve arrf(1 To mf)
- arrf(mf) = sPath & "" & File.Name '子文件写入数组
- Next
- If Folder.SubFolders.Count > 0 Then '如果还有子文件夹,则再调用本子程序(递归)
- For Each SubFolder In Folder.SubFolders
- Call GetFolders(SubFolder.Path, Fso, arrf, mf)
- Next
- End If
- Set Folder = Nothing
- Set SubFolder = Nothing
- End Sub
复制代码 |
|