ExcelTip.Net留存知识帖 ---【注:附件之前被网盘供应商清空后,现已修复-现已修复-现已修复为本地下载!】
现在位置:首页 > 我的酷贴 > Excel VBA > 自定义函数之获取目录

自定义函数之获取目录

作者:绿色风 分类: 时间:2022-08-18 浏览:87
楼主
DJ_Soo
之前写过不少获取目录的代码,但如果想以后再用的时候更方便的调用,当然是自定义代码最好,在此发布出来作为备份:
  1. Option Explicit

  2. '主程序
  3. Sub Main()
  4.     Dim arrList As Variant
  5.     Dim lstPath As String   '上次路径
  6.     Dim mPath As String     '选择的路径
  7.     Dim Fs As Object
  8.     Set Fs = CreateObject("scripting.filesystemobject")
  9.     lstPath = Sheets("tmp").[A1]
  10.     mPath = GetFolder("选择文件夹", lstPath)
  11.     Sheets("tmp").[A1] = mPath
  12.     arrList = Transpose(LlDirectory(mPath, Fs))
  13.     [A:C].ClearContents
  14.     [A1:C1] = Array("Type", "Size", "Path")
  15.     [A2].Resize(UBound(arrList), 3) = arrList
  16.     With [B:B]
  17.         .TextToColumns , xlGeneralFormat
  18.         .NumberFormat = "0 k\b"
  19.     End With
  20.     Set Fs = Nothing: End
  21. End Sub

  22. '获取对话框打开的文件所在地址,没有选择返回空,Title设置对话框的标题
  23. 'InitialFileName设置默认打开路径(可设置保留上次打开路径)
  24. Function GetFolder(Title As String, Optional InitialFileName As String) As String
  25.     Dim Folder As Object
  26.     Set Folder = Application.FileDialog(msoFileDialogFolderPicker)
  27.     With Folder
  28.         .Title = Title
  29.         .InitialFileName = InitialFileName
  30.         .AllowMultiSelect = False   '取消多选(没有针对多选写代码)
  31.         If .Show = -1 Then GetFolder = .SelectedItems(1) & "\"    '如果没有取消对话框,则获取地址
  32.     End With
  33.     Set Folder = Nothing
  34. End Function

  35. '获取下层目录Lower level directory
  36. Function LlDirectory(FolderPath As String, Fs As Object)
  37.     Dim F As Object     '获取当前folderpath的属性
  38.     Dim fll As Object   'f的属性中subfolder可以获取所有子文件夹
  39.     Dim fd As Object    'folder循环
  40.     Dim file As Object  'file循环
  41.     Static n As Long    'n获取文件的个数,static在递归时不初始化n为0
  42.     Static arr() As String  '存放文件路径'路径|size|属性(后缀)
  43.    
  44.     If FolderPath <> "" Then
  45.         Set F = Fs.GetFolder(FolderPath & "\")
  46.         Set fll = F.subfolders  '.subfolder获取子文件夹,.files获取所有文件
  47.         On Error Resume Next
  48.         n = n + 1
  49.         ReDim Preserve arr(1 To 3, 1 To n + F.Files.Count) As String
  50.         With F                      '文件夹列表处理
  51.             arr(3, n) = .Path & "\"
  52.             arr(2, n) = .Size
  53.             arr(1, n) = .Type
  54.         End With
  55.         For Each file In F.Files    '文件列表处理
  56.             n = n + 1
  57.             arr(3, n) = file.Path
  58.             arr(2, n) = file.Size
  59.             arr(1, n) = file.Type
  60.         Next file
  61.         For Each fd In fll          '递归,获取下层目录
  62.             Call LlDirectory(fd.Path, Fs)
  63.         Next fd
  64.         LlDirectory = arr
  65.     Else
  66.         MsgBox "没有选择文件夹!", vbInformation + vbOKOnly, "Error!"
  67.         End
  68.     End If
  69.     Set F = Nothing
  70.     Set fll = Nothing
  71.     Set fd = Nothing
  72. End Function

  73. '二维数组专用转置
  74. Function Transpose(arr As Variant)
  75.     Dim arrTmp() As String
  76.     Dim lstRo As Long
  77.     Dim Ro As Long
  78.     Dim lstCol As Long
  79.     Dim Col As Long
  80.     Dim lRo As Byte
  81.     Dim lCol As Byte
  82.     lstRo = UBound(arr, 1)
  83.     lstCol = UBound(arr, 2)
  84.     lRo = LBound(arr, 1)
  85.     lCol = LBound(arr, 2)
  86.     ReDim arrTmp(1 To lstCol - lCol + 1, 1 To lstRo - lRo + 1)
  87.     For Ro = 1 To lstRo - lRo + 1
  88.         For Col = 1 To lstCol - lCol + 1
  89.             arrTmp(Col, Ro) = arr(Ro + lRo - 1, Col + lCol - 1)
  90.         Next Col
  91.     Next Ro
  92.     Transpose = arrTmp
  93. End Function
说明在代码的注释中都有,不再赘述.上附件:

Upload.zip

2楼
xmyjk
学习了,漂亮。
3楼
俟人.琳
学习了,谢谢

免责声明

有感于原ExcelTip.Net留存知识的价值及部分知识具有的时间限定性因素, 经与ExcelTip.Net站长Apolloh商议并征得其同意, 现将原属ExcelTip.Net的知识帖采集资料于本站点进行展示, 供有需要的人士查询使用,也慰缅曾经的论坛时代。 所示各个帖子的原作者如对版权有异议, 可与本人沟通提出,或于本站点留言,我们会尽快处理。 在此,感谢ExcelTip.Net站长Apolloh的支持,感谢本站点所有人**绿色风(QQ:79664738)**的支持与奉献,特此鸣谢!
------本人网名**KevinChengCW(QQ:1210618015)**原ExcelTip.Net总版主之一

评论列表
sitemap