Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VBA Excel:在文件夹和子文件夹特定扩展名中搜索,并在Excel中写入标题_Excel_Vba - Fatal编程技术网

VBA Excel:在文件夹和子文件夹特定扩展名中搜索,并在Excel中写入标题

VBA Excel:在文件夹和子文件夹特定扩展名中搜索,并在Excel中写入标题,excel,vba,Excel,Vba,我需要创建一个VBA代码,以便在文件夹中搜索子文件夹文件扩展名('.csv'),并在excel工作簿中写入文件标题。我也遇到过类似的情况,我成功地做到了: Sub foo() Dim iFilesNum As Integer Dim iCount As Integer Dim recMyFiles() As FoundFileInfo Dim blFilesFound As Boolean Dim LastRow As Long With ActiveSheet LastRow =

我需要创建一个VBA代码,以便在文件夹中搜索子文件夹文件扩展名('.csv'),并在excel工作簿中写入文件标题。

我也遇到过类似的情况,我成功地做到了:

Sub foo()
Dim iFilesNum As Integer
Dim iCount As Integer
Dim recMyFiles() As FoundFileInfo
Dim blFilesFound As Boolean


Dim LastRow As Long
With ActiveSheet
    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With

For xy = 2 To LastRow

blFilesFound = FindFiles(Sheet1.Cells(xy, 1).Value, recMyFiles, iFilesNum, "*.csv", True) 'where column 1 on Sheet1 contains the Paths to be searched, include more rows with more paths to search through them too
If blFilesFound Then
    For iCount = 1 To iFilesNum
        With recMyFiles(iCount)
            Sheet2.Cells(iCount, 1).Value = .sPath & .sName 'place the results in Sheet2
        End With
    Next
Else
    MsgBox "No file(s) found matching the specified file spec.", vbInformation, "File(s) not Found"
End If

Next xy
MsgBox iFilesNum
End Sub
然后在模块中添加以下内容:

Type FoundFileInfo
    sPath As String
    sName As String
End Type

Function FindFiles(ByVal sPath As String, _
    ByRef recFoundFiles() As FoundFileInfo, _
    ByRef iFilesFound As Integer, _
    Optional ByVal sFileSpec As String, _
    Optional ByVal blIncludeSubFolders As Boolean = True) As Boolean

    Dim iCount As Integer           '* Multipurpose counter
    Dim sFileName As String         '* Found file name
    '*
    '* FileSystem objects
    Dim oFileSystem As Object, _
        oParentFolder As Object, _
        oFolder As Object, _
        oFile As Object

    Set oFileSystem = CreateObject("Scripting.FileSystemObject")
    On Error Resume Next
    Set oParentFolder = oFileSystem.GetFolder(sPath)
    If oParentFolder Is Nothing Then
        FindFiles = False
        On Error GoTo 0
        Set oParentFolder = Nothing
        Set oFileSystem = Nothing
        Exit Function
    End If
    sPath = IIf(Right(sPath, 1) = "\", sPath, sPath & "\")
    '*
    '* Find files
    sFileName = Dir(sPath & sFileSpec, vbNormal)
    If sFileName <> "" Then
        For Each oFile In oParentFolder.Files
            If LCase(oFile.Name) Like LCase(sFileSpec) Then
                iCount = UBound(recFoundFiles)
                iCount = iCount + 1
                ReDim Preserve recFoundFiles(1 To iCount)
                With recFoundFiles(iCount)
                    .sPath = sPath
                    .sName = oFile.Name
                End With
            End If
        Next oFile
        Set oFile = Nothing         '* Although it is nothing
    End If
    If blIncludeSubFolders Then
        '*
        '* Select next sub-forbers
        For Each oFolder In oParentFolder.SubFolders
            FindFiles oFolder.Path, recFoundFiles, iFilesFound, sFileSpec, blIncludeSubFolders
        Next
    End If
    FindFiles = UBound(recFoundFiles) > 0
    iFilesFound = UBound(recFoundFiles)
    On Error GoTo 0
    '*
    '* Clean-up
    Set oFolder = Nothing           '* Although it is nothing
    Set oParentFolder = Nothing
    Set oFileSystem = Nothing

End Function
键入FoundFileInfo
斯帕特作为一根绳子
作为字符串的sName
端型
函数FindFiles(ByVal sPath作为字符串_
ByRef recFoundFiles()作为FoundFileInfo_
ByRef IFilesFind为整数_
可选的ByVal sFileSpec作为字符串_
可选的ByVal blinclude子文件夹为Boolean=True)为Boolean
Dim I计数为整数'*多用途计数器
Dim sFileName作为字符串“*找到文件名
'*
'*文件系统对象
作为对象的系统的尺寸_
oParentFolder作为对象_
以文件夹为对象_
以文件为对象
leSystem=CreateObject的集合(“Scripting.FileSystemObject”)
出错时继续下一步
设置oParentFolder=oFileSystem.GetFolder(sPath)
如果oParentFolder为空,则
FindFiles=False
错误转到0
设置oParentFolder=Nothing
leSystem的集合=无
退出功能
如果结束
sPath=IIf(右(sPath,1)=“\”,sPath,sPath&“\”)
'*
“*查找文件
sFileName=Dir(sPath&sFileSpec,vbNormal)
如果是sFileName“”,则
对于oParentFolder.Files中的每个文件
如果LCase(oFile.Name)与LCase(sFileSpec)相似,那么
iCount=UBound(recFoundFiles)
iCount=iCount+1
重拨保留recFoundFiles(1到iCount)
使用recFoundFiles(iCount)
.sPath=sPath
.sName=oFile.Name
以
如果结束
下一个文件
文件集=Nothing'*尽管它是Nothing
如果结束
如果包含子文件夹,则
'*
'*选择下一个子福布斯
对于oParentFolder.SubFolders中的每个文件夹
查找文件夹.Path、recFoundFiles、iFilesFound、sFileSpec和blIncludeSubFolders的文件
下一个
如果结束
FindFiles=UBound(recFoundFiles)>0
iFilesFound=UBound(recFoundFiles)
错误转到0
'*
*清理
文件夹集=Nothing'*尽管它是Nothing
设置oParentFolder=Nothing
leSystem的集合=无
端函数

您需要一名合格的员工,stackoverlow不是免费的编码服务。请阅读:。请展示您尝试过的内容。@V.MCR如果对您有帮助,请将我的回复标记为asnwer好吗?非常感谢。