Friday, July 10, 2009

Excel VBA: Search And Replace in different files

Sub SearchAndReplace()
Dim oFSO As New FileSystemObject
Dim oFileIn As TextStream
Dim oFileOut As TextStream
Dim sLine As String

If Not oFSO.FileExists("C:\Test\Try04.txt") Then
MsgBox "File Not Found", vbCritical
Exit Sub
End If

Set oFileIn = oFSO.OpenTextFile("C:\Test\Try04.txt")
Set oFileOut = oFSO.CreateTextFile("C:\Test\Try05.txt")

Do While Not oFileIn.AtEndOfStream
sLine = oFileIn.ReadLine
sLine = Replace(sLine, "4", "X")

'sLine = Replace(oFileIn.ReadLine, "4", "X")

oFileOut.WriteLine sLine

Loop
oFileIn.Close
oFileOut.Close

Set oFileIn = Nothing
Set oFileOut = Nothing
Set oFSO = Nothing

MsgBox "Over", vbInformation
End Sub

No comments:

Post a Comment