双击单元格进行搜索
这是我从各个地方组合在一起的解决方案,以在路径上打开资源管理器窗口,使用 Windows 文件资源管理器搜索功能按所选单元格中的术语过滤(搜索)。它由双击包含搜索词的单元格触发:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Application.Intersect(Target, Range("A1:AA1048576")) Is Nothing Then
Dim d As String
Dim searchpath As String
Dim searchlocation As String
Cancel = True
d = Selection.Value
'change window name to make sure new explorer window is opened for each instance
'copy string from manual search
searchpath = "search-ms:displayname=" & d & "%20Results%20&crumb=System.Generic.String%3A"
'copy string from manual search (e.g. my documents replace USERNAME)
searchlocation = "&crumb=location:C%3A%5CUsers%5CUSERNAME%5CDocuments"
If Not d = "" Then
Call Shell("explorer.exe """ & searchpath & d & searchlocation & "", 1)
'src: https://stackoverflow.com/questions/24376850/open-explorer-search-from-excel-hyperlink
End If
End If
End Sub
这将在 VbNormalFocus 中打开窗口,窗口标题设置为单元格变量 (d)。确保如果此代码在另一个单元格值上运行,则会打开一个新的单独窗口。如果没有这个,我发现下次运行代码时,资源管理器窗口没有更新为新的搜索值,而是将焦点转移到上一个结果。
编辑:“从搜索栏复制”是位置之后的字符串:在资源管理器中手动搜索的地址栏中
使用 ActiveX 控件
添加一个 ActiveX 文本框 (TextBox1) 和按钮 (CommandButton1) 并将以下代码添加到命令按钮:
Private Sub CommandButton1_Click()
Dim d As String
Dim searchpath As String
Dim searchlocation As String
Cancel = True
d = TextBox1.Value
'change window name to make sure new explorer window is opened for each instance
'copy string from manual search
searchpath = "search-ms:displayname=" & d & "%20Results%20&crumb=System.Generic.String%3A"
'copy string from manual search (e.g. my documents replace USERNAME)
searchlocation = "&crumb=location:C%3A%5CUsers%5CUSERNAME%5CDocuments"
If Not d = "" Then
Call Shell("explorer.exe """ & searchpath & d & searchlocation & "", 1)
'src: https://stackoverflow.com/questions/24376850/open-explorer-search-from-excel-hyperlink
End If
End Sub
现在用户可以更改文本框中的文本,点击按钮会打开windows文件资源管理器搜索代码中指定的文件夹。
Screenshot example using button search for "Editable Search Text"
编辑
您可以使用 Windows 搜索语法包含其他搜索功能:
http://download.microsoft.com/download/8/1/7/8174a74e-3d8d-4478-abc6-84cd51ad93c4/Windows_Desktop_Advanced_Query_Reference.pdf
例如。您可以通过更改搜索变量 "d:
在文件夹中搜索与字符串中每个单词部分匹配的所有文件
...
d = Selection.Value
d = "(" & Replace(d, " ", " OR ") & ")"
...
如果选择 (d) 的值为 Where will I find it
这将在 Windows 资源管理器中搜索 (Where OR will OR I OR find OR it),并返回名称为 WHEREver 和 Last WILL and testament 的文件。我发现这对定性信息很有用,其中可以接受更广泛的搜索,并且用户可以轻松过滤(注意:上面的示例还将返回名称包含 i 的所有文件,所以它不是很具体!)