Hello,
I've tried hard to find a solution for my problem but all the codes I've tried don't work or something is missing.
What I need is a Macro that finds a given string inside a .txt file in a given directory (more than 100 files in it) and then copy the file to a specific path.
The code I have is:
I cannot make it work, do you think you can help me with this?
Thank you!
I've tried hard to find a solution for my problem but all the codes I've tried don't work or something is missing.
What I need is a Macro that finds a given string inside a .txt file in a given directory (more than 100 files in it) and then copy the file to a specific path.
The code I have is:
Code:
Sub StringExistsInFile()
Dim theString As String
Dim path As String
Dim StrFile, NewFile As String
Dim fso As New FileSystemObject
Dim file As TextStream
Dim line As String
theString = Range("K3").Value
path = Range("K4").Value
StrFile = Dir(path & "*.txt")
Do While StrFile <> ""
Set file = fso.OpenTextFile(path & StrFile)
Do While Not file.AtEndOfLine
line = file.ReadLine
If InStr(1, line, theString, vbTextCompare) > 0 Then
FileCopy path & StrFile, "C:\MyData\" & StrFile
Exit Do
Else
End If
Loop
file.Close
Set file = Nothing
Set fso = Nothing
StrFile = Dir()
Loop
End Sub
I cannot make it work, do you think you can help me with this?
Thank you!