|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
本帖最后由 百度不到去谷歌 于 2014-8-24 21:59 编辑
几分钟就改好了 另外拜托不要乱叫版主 我可不是版主!!这个不用设置搜索按钮了 文本框输进去 即时搜索
- Option Compare Text
- Sub TvInPut(ByVal TV As TreeView, arr)
- '从数组读取数据写入到目录树
- Dim i&, parKey$, k
- Dim nodex As Node
- Set TV.ImageList = Nothing
- TV.Nodes.Clear
- If arr(1) = "" Then Exit Sub
- 'TV.Nodes.Add , , "VBA代码助手", "VBA代码助手" '加入根节点
- For i = 1 To UBound(arr)
- TvInPutIterate TV, arr(i)
- Next
- TV.Nodes(1).EnsureVisible
- End Sub
- Sub TvInPutIterate(ByVal TV As TreeView, key) '递归构造树
- '从数组读取数据写入到目录树
- Dim i&, parKey$, k
- 'TV.Nodes.Add , , "VBA代码助手", "VBA代码助手" '加入根节点
- k = InStrRev(key, "-")
- If k = 0 Then '顶层
- TV.Nodes.Add(, , key, key).EnsureVisible
- Else
- parKey = Left(key, k - 1) '获得父节点关键字
- On Error Resume Next
- TV.Nodes(parKey).Tag = parKey
- If Err <> 0 Then '父节点不存在会发生错误,则递归向前构造父节点
- TvInPutIterate TV, parKey
- Err.Clear
- End If
- On Error GoTo 0
- TV.Nodes.Add(parKey, tvwChild, key, Mid(key, k + 1)).EnsureVisible
- End If
- End Sub
- Private Sub TextBox1_Change()
- Call TvInPut(TreeView1, filelist(ThisWorkbook.path & "\文件", TextBox1))
- End Sub
- Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
- On Error Resume Next '复制节点对应的文件
- Dim path$
- path = ThisWorkbook.path & "\文件" + Node.FullPath + ".xls"
- Application.ScreenUpdating = False
- If Dir(path) <> "" Then
- With GetObject(path)
- .Sheets(1).Cells.Copy ThisWorkbook.Sheets("样表").Cells
- .Close
- End With
- End If
- Application.ScreenUpdating = True
- End Sub
- Private Sub UserForm_Initialize()
- TreeView1.PathSeparator = "-"
- TvInPut TreeView1, filelist(ThisWorkbook.path + "\文件", "") 'tree初始化
- TreeView1.Sorted = True
- End Sub
- Private Function filelist(folderspec, ByVal p$)
- Dim fs, f, f1, fc, s, arr, i
- Set fs = CreateObject("Scripting.FileSystemObject")
- Set f = fs.GetFolder(folderspec)
- Set fc = f.Files
- ReDim arr(1 To fc.Count)
- i = 0
- For Each f1 In fc
- If f1.Name Like ("*" + p + "*") Then i = i + 1: arr(i) = Left(f1.Name, Len(f1.Name) - 4)
- Next
- If i = 0 Then i = 1
- ReDim Preserve arr(1 To i)
- filelist = arr
- Set fs = Nothing
- End Function
复制代码
treeview与文件夹目录.rar
(47.98 KB, 下载次数: 331)
|
|