Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.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:在windows资源管理器中搜索和筛选可变文件夹,选择文件夹并导入其文件_Excel_Vba - Fatal编程技术网

VBA/Excel:在windows资源管理器中搜索和筛选可变文件夹,选择文件夹并导入其文件

VBA/Excel:在windows资源管理器中搜索和筛选可变文件夹,选择文件夹并导入其文件,excel,vba,Excel,Vba,目标是能够基于Excel中的选定单元格及其下方的“分类”单元格执行目录搜索,并返回目录中名称包含此选定信息的所有文件夹。从那里,我希望能够从该列表中选择一个文件夹,并将其文本文件导入到原始选定单元格旁边的excel工作表中 理想情况下,我希望它在一个循环中工作,在这个循环中,它将重复这个过程四次,并将数据从四个不同的文件夹导入到新的列中。所有这些都是为了自动化一些数据比较 据我所知,在VBA的Application.FileDialog(msoFileDialogFolderPicker)函数中

目标是能够基于Excel中的选定单元格及其下方的“分类”单元格执行目录搜索,并返回目录中名称包含此选定信息的所有文件夹。从那里,我希望能够从该列表中选择一个文件夹,并将其文本文件导入到原始选定单元格旁边的excel工作表中

理想情况下,我希望它在一个循环中工作,在这个循环中,它将重复这个过程四次,并将数据从四个不同的文件夹导入到新的列中。所有这些都是为了自动化一些数据比较

据我所知,在VBA的
Application.FileDialog(msoFileDialogFolderPicker)
函数中无法过滤文件夹结果,因此我一直在尝试找到一种解决方法。使用其他文章中的一些代码,我已经能够让VBA重新创建搜索函数并在资源管理器窗口中将其拉出来,但是,我不知道如何在文件导入函数中使用此搜索字符串。 这是我当前的代码,它可以让我进入所需的筛选文件夹窗口:

Sub SearchExplorerForSelection()

Dim d As String
Dim searchpath As String
Dim searchlocation As String

Dim PartNumber As Range
Dim GenType As Range
' Cancel = True
d = Selection.Value

Set PartNumber = Selection 'Get desired part number from selected cell
Set GenType = PartNumber.Offset(2) 'Get PN's classification
PartNumberSearch = GenType & "*" & PartNumber 'Set full search keywords

searchpath = "search-ms:displayname=Search%20Results%20in%20" & GenType & "&crumb=filename%3A~" & PartNumberSearch
'copy string from manual search (e.g. my documents replace USERNAME)

searchlocation = "%20OR%20System.Generic.String%3A" & PartNumberSearch & "&crumb=location:Z%3A%5CTest%5CCalibration_Data_Generators%5C" & GenType
If Not d = "" Then
    Call Shell("explorer.exe """ & searchpath & searchlocation, 1)
   'src: https://stackoverflow.com/questions/24376850/open-explorer-search-from-excel-hyperlink

End If

End Sub

我对使用VBA非常陌生。

编辑-我想我可能误解了你的问题,因为我问的是关于搜索文件内容的问题,但实际上是关于按文件夹名称搜索的问题

我认为在这种情况下,最好使用Dir()或FileSystemObject循环遍历“根”文件夹下的所有文件夹,并在列表框中向用户返回匹配文件夹名称的列表,该列表框可以位于工作表或用户表单上。然后他们可以从列表中选择



基于:

非常感谢。你能指导我们测试代码的步骤吗?我已经在桌面上创建了临时文件夹,创建了一些文件夹,并在这些文件夹中创建了已验证的文件夹。我是否遗漏了什么?是的-我(错误地)发布的关于使用Windows Search的内容是基于我错误地阅读了OP的问题。我发布的内容是在“C:\Users\blahblah\Desktop\Temp”文件夹下查找包含(例如)单词“validated”的文件。非常感谢您的支持,它运行良好,似乎没有遗漏任何内容。您有没有建议学习如何将其集成到listbox/userform中的资源?
Sub Tester2()

    Dim col As Collection, f

    Set col = GetFolderMatches("C:\Users\blahblah\Stuff", "Mail")

    For Each f In col
        Debug.Print f  '<< add to a list for the user to pick from
    Next f
End Sub



'Return a collection of folder paths given a starting folder and a term to search on
'  e.g. "*.txt"
'Pass False for last parameter if don't want to check subfolders
Function GetFolderMatches(startFolder As String, nameIncludes As String, _
                    Optional subFolders As Boolean = True) As Collection

    Dim fso, fldr, f, subFldr
    Dim colFolders As New Collection
    Dim colSub As New Collection

    Set fso = CreateObject("scripting.filesystemobject")
    colSub.Add startFolder

    Do While colSub.Count > 0
        Set fldr = fso.getfolder(colSub(1))
        colSub.Remove 1
        If LCase(fldr.Name) Like "*" & LCase(nameIncludes) & "*" Then
            colFolders.Add fldr.Path
        End If
        If subFolders Then
            For Each subFldr In fldr.subFolders
                colSub.Add subFldr.Path
            Next subFldr
        End If
    Loop
    Set GetFolderMatches = colFolders
End Function
Sub Tester()

    Dim conn As New ADODB.Connection, rs As ADODB.Recordset

    conn.Open "Provider=Search.CollatorDSO;Extended Properties='Application=Windows';"

    Set rs = conn.Execute("SELECT System.ItemPathDisplay " & _
                          " FROM SYSTEMINDEX WHERE " & _
                          " SCOPE = 'C:\Users\blahblah\Desktop\Temp' " & _
                          " and contains('validated')")

    Do While Not rs.EOF
        Debug.Print rs(0).Value
        rs.MoveNext
    Loop

End Sub