I am only posting this as a demonstration that this can be done. I am by no means stating that this will work for anything other than my small test I have done with the code.
- It will ask for the source file path
- It will ask for a full path for the output file it's going to create
- It will ask the string you want to find. I used "test"
- It will ask the string you want to replace it with. I used "TEST"
- It will ask you if there's a delimiter between data in the rows
- It will process the data until it's done. (I was too lazy to add any error checking which is the reason for the disclaimer below)
VERY BIG DISCLAIMER: USE AT YOUR OWN RISK
If you want to test this, I suggest copying the file and run the test on a duplicate so the original remains unaffected.
Macro:
Dim FilePath As String
Dim FilePathTo As String
Dim findthis As String
Dim replwiththis As String
Dim delimiter As String
FilePath = InputBox("What is the full path of your .txt file?")
If Dir(FilePath) = "" Then
MsgBox "File does not exist, exiting"
End
End If
FilePath2 = InputBox("What do you want to call the output file? (new file)")
If Dir(FilePath2) <> "" Then
MsgBox "File Already Exists, exiting"
End
End If
findthis = InputBox("What value do you want to find?")
replwiththis = InputBox("What value do you want to replace it with?")
delimiter = InputBox("What is the delimiter? (Enter nothing if none)")
Open FilePath2 For Output As #1
Open FilePath For Input As #2
Do Until EOF(2)
Line Input #2, LineFromFile
Dim newline As String
LineItems = Split(LineFromFile, delimiter)
If UBound(LineItems) >= 0 Then
For i = LBound(LineItems) To UBound(LineItems)
If LineItems(i) = findthis Then
LineItems(i) = replwiththis
End If
Next i
newline = Join(LineItems, delimiter)
Print #1, newline
Else
If Len(LineFromFile) < 1 Then
Print #1, ""
Else
Print #1, LineFromFile
End If
End If
Loop
Close #2
Close #1
MsgBox ("Done")
Input File:
test,test1,test2,test3
test4,test5,test4,test3
test2,test1,test,test
Output File:
TEST,test1,test2,test3
test4,test5,test4,test3
test2,test1,TEST,TEST
Bookmarks