0

I am using excel vba to find a record in csv files which matches given string I need help in searching through all csv files in a folder for a particular string and get the file name which contains the search string and from within the file the row itself. The string will always appear in column B of the CSV. Thanks in advance

1
  • If you have any code you should post it. If you haven't yet tried anything, try using Dir(pathToYourFolder & "*.csv") to loop through and open each file in the source folder. Plenty of examples of this here on SO alone. Commented Mar 13, 2014 at 16:48

2 Answers 2

6

It might be easier to start a Command Prompt and navigate to your folder containing the CSV files. Then you can do this:

FINDSTR /N /I "something" *.CSV

where "something" is whatever you are looking for.

Sign up to request clarification or add additional context in comments.

3 Comments

How can i get this into VBA?
My point was that this probably easier than loading a load of files in Excel by clicking around all over the place.
seriously saved my time, ive been trying to bang head with powershell
0

This returns the first file it finds the match, found here 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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.