2

I need help in creating find and replace string macro so that it can do find and replace string in all files in a folder.

For example fofler = "C:\ifolder\" files list = "*.xlsx"

so far I can only do it for one file, I need to do it for all file in a folder

Sub ReplaceStringInFile()

Dim sBuf As String
Dim sTemp As String
Dim iFileNum As Integer
Dim sFileName As String

' Edit as needed
sFileName = "C:\macro\test.txt"

iFileNum = FreeFile
Open sFileName For Input As iFileNum

Do Until EOF(iFileNum)
    Line Input #iFileNum, sBuf
    sTemp = sTemp & sBuf & vbCrLf
Loop
Close iFileNum

sTemp = Replace(sTemp, "THIS", "THAT")

iFileNum = FreeFile
Open sFileName For Output As iFileNum
Print #iFileNum, sTemp
Close iFileNum

End Sub
4
  • 2
    please show what you tried and where you're stuck Commented Feb 12, 2013 at 19:54
  • 1
    The very same trouble made me learn VBA 4 months ago from complete zero-level))) Commented Feb 12, 2013 at 20:57
  • 1
    You can help get an answer by providing more detail - what exactly are you having problems with? Do you know any VBA at all? Do you need to know how to loop though files in a folder? Open a workbook? Commented Feb 12, 2013 at 21:54
  • 1
    stackoverflow.com/questions/10380312/… Commented Feb 12, 2013 at 23:02

1 Answer 1

3

As you actually have code that opens text files - not Excel files - I have followed the same approach

Something like this where

  1. Dir is used to loop through all files in a specific folder.
  2. Use the FileScriptingObject to read in all the text at once, make the replacement, then write over the file file with the updated text.

code

Sub  ReplaceStringInFile()

Dim objFSO As Object
Dim objFil As Object
Dim objFil2 As Object
Dim StrFileName As String
Dim StrFolder As String
Dim SstrAll As String

Set objFSO = CreateObject("scripting.filesystemobject")
StrFolder = "c:\macro\"
StrFileName = Dir(StrFolder & "*.txt")

Do While StrFileName <> vbNullString
    Set objFil = objFSO.opentextfile(StrFolder & StrFileName)
    strAll = objFil.readall
    objFil.Close
    Set objFil2 = objFSO.createtextfile(StrFolder & StrFileName)
    objFil2.Write Replace(strAll, "THIS", "THAT")
    objFil2.Close
    StrFileName = Dir
Loop
End Sub
Sign up to request clarification or add additional context in comments.

5 Comments

dear thanks for the code. As, I am new to VBA can you please help me where to paste this code and run. I tried it by pasting in VBAProject->ThisWorkBook. It is not showing any error while running but at the same time it is not replacing i.e. not working. Can you please help.
@guaravkumar place it in a regular code module. Ensure your path name has been updated (from C;\macro\ above).
Many thanks for the help. I am doing it for the excel files. I think this is the reason for the error: Input past end of file (Error 62). Can you please help me in writting the subsitute for opentextfile and createtextfile as used in the program. Many thanks in anticipation :)
@gauravkumar suggest you ask a new question :)
I have asked a new question stackoverflow.com/questions/32744968/…

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.