【问题标题】:How to search through csv files in a folder for a string through excel vba?如何通过excel vba在文件夹中的csv文件中搜索字符串?
【发布时间】:2014-03-13 16:41:16
【问题描述】:

我正在使用 excel vba 在 csv 文件中查找与给定字符串匹配的记录 我需要帮助在文件夹中的所有 csv 文件中搜索特定字符串并获取包含搜索字符串的文件名,并从文件中获取行本身。该字符串将始终出现在 CSV 的 B 列中。提前致谢

【问题讨论】:

  • 如果你有任何代码,你应该发布它。如果您还没有尝试过任何操作,请尝试使用Dir(pathToYourFolder & "*.csv") 循环并打开源文件夹中的每个文件。仅在 SO 上就有很多这样的例子。

标签: vba excel csv


【解决方案1】:

启动命令提示符并导航到包含 CSV 文件的文件夹可能会更容易。然后你可以这样做:

FINDSTR /N /I "something" *.CSV

“某物”是您要寻找的任何东西。

【讨论】:

  • 我怎样才能把它放到 VBA 中?
  • 我的意思是,这可能比在 Excel 中到处点击加载大量文件更容易。
  • 严重节省了我的时间,我一直在尝试用 powershell 敲头
【解决方案2】:

这将返回找到匹配项的第一个文件,在此处找到 http://www.ggkf.com/excel/vba-macro-to-search-a-folder-for-keyword

Sub loopThroughFiles()
Dim file As String
file = FindFiles("putyourpathname eg.c:\reports", "search string")
If (file <> "") Then MsgBox file
End Sub
Function FindFiles(ByVal path As String, ByVal target As String) As String
' Run The Shell Command And Get Output
Dim files As String
files = CreateObject("Wscript.Shell").Exec("FINDSTR /M """ & target & """ """ & path &     "\*.*""").StdOut.ReadAll
FindFiles = ""
If (files <> "") Then
    Dim idx As Integer
    idx = InStr(files, vbCrLf)
    FindFiles = Left(files, idx - 1)
End If
End Function

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-09
    • 1970-01-01
    相关资源
    最近更新 更多