Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 5815|回复: 13

[已解决]求当前文件夹名和文件名显示在TreeView目录树代码

[复制链接]
发表于 2013-2-3 15:16 | 显示全部楼层 |阅读模式
当前文件夹内有若干个子文件夹,每个子文件夹内有不等个文件,求各位大侠赐VB代码,把文件夹内的子文件夹名显示在一级目录里,打开一级目录,显示每个文件夹内指定格式的文件名
最佳答案
2013-2-4 19:42
本帖最后由 hwc2ycy 于 2013-2-19 14:33 编辑

改了下。
  1. Option Explicit

  2. Private Sub CommandButton1_Click()
  3.     Dim Path$
  4.     Me.TreeView1.Visible = False
  5.     With Application.FileDialog(msoFileDialogFolderPicker)
  6.         .AllowMultiSelect = False
  7.         .InitialFileName = ThisWorkbook.Path
  8.         .Show
  9.         If .SelectedItems.Count = 1 Then Path = .SelectedItems(1)
  10.     End With
  11.     If Len(Path) Then
  12.         With Me.TreeView1
  13.             .Visible = False
  14.             .Nodes.Clear
  15.             .ImageList = Me.ImageList1
  16.             Call ListDir(Path)
  17.             .Visible = True
  18.         End With
  19.     End If
  20. End Sub

  21. Sub ListDir(ByVal Path As String)
  22.     Dim filename$, root As Node
  23.     Dim arrPath()
  24.     Dim sPath$
  25.     Dim i&, j&
  26.     i = 1
  27.     j = 1
  28.    
  29.     ReDim arrPath(1 To 1)
  30.     arrPath(i) = Path & Application.PathSeparator
  31.     'On Error Resume Next
  32.    
  33.     With Me.TreeView1
  34.         sPath = arrPath(j)
  35.         Debug.Print sPath
  36.         Set root = .Nodes.Add(, , sPath, sPath, 1)
  37.         Do While Len(sPath)
  38.             filename = Dir(sPath & "*.*", vbDirectory + vbNormal)
  39.             Do While Len(filename)
  40.                
  41.                 If Not (filename = "." Or filename = "..") Then
  42.                     If (GetAttr(sPath & "" & filename) And vbDirectory) = 16 Then
  43.                         '避免读取错误
  44.                         If Err.Number <> 0 Then Err.Clear: GoTo End1If
  45.                         .Nodes.Add sPath, 4, sPath & filename & Application.PathSeparator, filename, 1
  46.                         i = i + 1
  47.                         ReDim Preserve arrPath(1 To i)
  48.                         arrPath(i) = sPath & filename & Application.PathSeparator
  49.                     Else
  50.                         .Nodes.Add sPath, 4, sPath & filename, filename, 2
  51.                     End If
  52.                 End If
  53. End1If:
  54.                 filename = Dir
  55.             Loop
  56.             j = j + 1
  57.             If j > i Then Exit Do
  58.             sPath = arrPath(j)
  59.         Loop
  60.     End With
  61. End Sub

  62. Private Sub TreeView1_BeforeLabelEdit(Cancel As Integer)
  63.     Cancel = True
  64. End Sub
复制代码
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2013-2-3 15:35 | 显示全部楼层
遍历文件夹,要是开太多会吃不开的。
回复

使用道具 举报

 楼主| 发表于 2013-2-3 16:18 | 显示全部楼层
回复

使用道具 举报

发表于 2013-2-3 17:18 | 显示全部楼层
本帖最后由 hwc2ycy 于 2013-2-4 19:43 编辑

Treeview.rar (45.31 KB, 下载次数: 78)
回复

使用道具 举报

 楼主| 发表于 2013-2-3 17:20 | 显示全部楼层
hwc2ycy 谢谢了!
回复

使用道具 举报

 楼主| 发表于 2013-2-3 17:22 | 显示全部楼层
麻烦您在附件里写下代码好吗!谢谢!
回复

使用道具 举报

发表于 2013-2-3 17:25 | 显示全部楼层
代码一直就有呀。
Treeview.rar (42.11 KB, 下载次数: 74)
回复

使用道具 举报

 楼主| 发表于 2013-2-3 17:26 | 显示全部楼层
十分感谢,辛苦了!我试下代码
回复

使用道具 举报

发表于 2013-2-3 19:19 | 显示全部楼层
Treeview.rar (43.5 KB, 下载次数: 182)
回复

使用道具 举报

 楼主| 发表于 2013-2-3 19:24 | 显示全部楼层
麻烦您吧代码贴出来,我还是2003版本的,下了老半天兼容程序,网速慢!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-27 04:07 , Processed in 0.375517 second(s), 11 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表