【问题标题】:Excel VBA code to capture all file names from Parent Folder into excel sheetExcel VBA代码将父文件夹中的所有文件名捕获到excel表中
【发布时间】:2021-05-19 01:07:01
【问题描述】:

我正在使用 VBA 代码将所有文件名从父文件夹捕获到 excel 中,下面的代码对我来说只能捕获一种文件格式,即.xlsm。 有没有办法捕获多种格式的文件,例如 PDF、JPEG、docx。

以下是 VBA 代码:-

Sub getallfiles()

    Dim fso As Object
    Dim parentfolder As Object
    Dim folder As Object
    Dim file As Object
    Dim filetype As String
    i = 1
    Filelocation = "C:\Users\USER\Desktop\VBA Practice"
    Range("A2").Select
    If ActiveCell.Value <> "" Then
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlToRight)).Select
        Selection.ClearContents
    End If
    Set fso = CreateObject("scripting.filesystemobject")
    Set parentfolder = fso.Getfolder(Filelocation)
    filetype = "*.xlsm*"
    Listallfiles parentfolder,filetype
    For Each folder In parentfolder.Subfolders
        Listallfiles folder, filetype
        Next
End Sub

【问题讨论】:

  • 什么是ListAllFiles?请编辑您的问题以提供minimal reproducible example
  • 显而易见的是更改您的文件类型参数。你试过什么?
  • Sub Listallfiles(fld As Object, ftype As Variant, Sh As Works) Dim file as Object For each file in fld.Files If file.Name Like ftype Then ActiveCell.Value = i ActiveCell.Offset( 0, 1).Value = file.Name ActiveCell.Offset(0, 2).Value = fld.Path If Right(fld.Path, 8) = "Treasury" Then ActiveCell.Offset(0, 3).Value = "是" Else ActiveCell.Offset(0, 3).Value = "" End If ActiveCell.Offset(1, 0).Select i = i + 1 End If Next End Sub
  • @Fazil Oleed 在评论中发布代码不是一个好主意。如果需要/需要,请编辑您的问题并将代码放在那里...
  • 著名的Fane Duru

标签: excel vba


【解决方案1】:

你没有向我们展示被调用的函数,我试着想象一个能够做你需要的事情。请用下一种方式测试你的代码:

Sub getallfiles()
 Dim fso As Object, parentfolder As Object, folder As Object, file As Object
 Dim fileType As Variant, Filelocation As String

 Filelocation = "C:\Users\USER\Desktop\VBA Practice" 'please use your real USER...
 Range("A2").Select
 If ActiveCell.Value <> "" Then
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.ClearContents
 End If
 Set fso = CreateObject("scripting.filesystemobject")
 Set parentfolder = fso.GetFolder(Filelocation)
 fileType = Array("xlsm", "pdf", "jpg", "docx") 'put here how many extensions you wang...
 ListAllFiles parentfolder, fileType, ActiveSheet
 For Each folder In parentfolder.SubFolders
     ListAllFiles folder, fileType, ActiveSheet
 Next
End Sub

Sub ListAllFiles(strFold As Object, fileType As Variant, sh As Worksheet)
 Dim fso As Object, objFile As Object, lastR As Long, mtch As Variant
 
    Set fso = CreateObject("scripting.filesystemobject")
    
    lastR = sh.Range("A" & sh.rows.count).End(xlUp).row + 1
        For Each objFile In strFold.files
           mtch = Application.match(fso.GetExtensionName(objFile.name), fileType, 0)
           If Not IsError(mtch) Then
                sh.Range("A" & lastR).Value = objFile: lastR = lastR + 1
           End If
        Next
End Sub

上面的代码可以使用一些优化,以便更快。最快的方法应该是使用一个数组,将它填充到被调用的 sub 中并在最后一次删除它的值......

已编辑

  1. 请在代码所在的模块顶部声明下一个变量(在声明区域中):
Private arrFiles As Variant, iFile As Long
  1. 在您声明上述变量的标准模块中复制下一个改编代码:
Sub getallfilesArrray() 
 Dim fso As Object, parentfolder As Object, folder As Object, file As Object
 Dim fileType As Variant, Filelocation As String

 Filelocation = "C:\Users\USER\Desktop\VBA Practice"
 Range("A2").CurrentRegion.ClearContents

 Set fso = CreateObject("scripting.filesystemobject")
 iFile = 1
 ReDim arrFiles(1 To 2, 1 To 1000) 'if the estimation for existing files is bigger, use a bigger number
 Set parentfolder = fso.GetFolder(Filelocation)
 fileType = Array("xlsm", "pdf", "jpg", "docx")
 ListAllFilesA parentfolder, fileType
 For Each folder In parentfolder.SubFolders
     ListAllFilesA folder, fileType
 Next
 ReDim Preserve arrFiles(1 To 2, 1 To iFile - 1)
 With Range("A2").Resize(iFile - 1, 2)
    .Value = Application.Transpose(arrFiles)
    .EntireColumn.AutoFit
 End With
 MsgBox "Ready..."
End Sub

Sub ListAllFilesA(strFold As Object, fileType As Variant)
 Dim fso As Object, objFile As Object, mtch As Variant
 
    Set fso = CreateObject("scripting.filesystemobject")
    
        For Each objFile In strFold.files
           mtch = Application.match(fso.GetExtensionName(objFile.name), fileType, 0)
           If Not IsError(mtch) Then
                arrFiles(1, iFile) = objFile
                arrFiles(2, iFile) = objFile.name
                iFile = iFile + 1
           End If
        Next
End Sub

【讨论】:

  • @Fazil Oleed 现在没关系。请使用数组测试更新的解决方案,它几乎立即返回,即使对于大量子文件夹和文件也是如此......我认为用我发布的内容调整现有版本应该很容易。仅声明具有必要列数的数组...
  • 感谢您的支持。我是 VBA 的初学者,所以在理解代码方面有些困难。
  • FaneDuru ,是否也可以包含文件名?
  • 我现在正在开车。但是代码返回完整的文件名。您只需要不同列中的文件名吗?不确定我得到你...
  • 没关系,慢慢来。是的,我需要不同列中的文件名
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-14
  • 1970-01-01
  • 1970-01-01
  • 2012-03-16
相关资源
最近更新 更多