|
本帖最后由 Kelidai 于 2012-6-1 14:33 编辑
给你一段代码参考吧:- Private Sub CommandButton2_Click()
- Application.ScreenUpdating = False
- Application.Calculation = xlCalculationManual
- Dim i As Integer
- Dim j As Integer
- Sheet1.Hyperlinks.Delete
- Sheet1.Columns("B:B").HorizontalAlignment = xlCenter
- Dim MyName, Dic, Did, T, F, TT, MyFileName, objShell, objFolder, lj, Ke, sz, Sh, rng As Range, cell As Range
- lj = "\\CH3DDC3\operationalteam\03_PO Report\2012 PO"
- 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
- Did.Add ("文件清单"), ""
- For Each Ke In Dic.keys
- MyFileName = Dir(Ke & "*.*")
- Do While MyFileName <> ""
- Did.Add (Ke & MyFileName), ""
- MyFileName = Dir
- Loop
- Next
- Sh = Did.keys
- For i = 3 To Sheet1.Range("a65535").End(xlUp).Row
- For j = 0 To Did.Count - 1
- If Sh(j) Like "*" & Sheet1.Cells(i, 2).Value & "*.*" Then
- Sheet1.Hyperlinks.Add anchor:=Sheet1.Cells(i, 2), Address:=Sh(j)
- Exit For
- End If
- Next j
- Next i
- Application.ScreenUpdating = True
- Application.Calculation = xlCalculationAutomatic
- End Sub
复制代码 |
|