Following should do it then - Add to a module
Option Explicit
Option Compare Text
Public Sub EditChat()
'// For simplicity - can easily be extended to browse
'// for the file but in the meantime edit to point
'// to the actual file with the full directory path
Const ChatFile As String = "C:\Users\Fred\Downloads\chat.txt"
Dim iFile As Integer
Dim ChatText As String
Dim r As Excel.Range
Dim ws As Excel.Worksheet
'// Get a free file handle
iFile = FreeFile
'// Open the chat file for input
Open ChatFile For Input As #iFile
'// and read the entire contents to a variable
ChatText = Input(LOF(iFile), #iFile)
'// Done with - close the file.
Close #iFile
'// Loop each used cell in Column A
Set ws = ActiveSheet
For Each r In ws.Range("A1:A" & ws.Cells(Rows.Count, 1).End(xlUp).Row)
'// If both Col A & Col B contain text...
If r.Value <> vbNullString And r.Offset(, 1).Value <> vbNullString Then
'// Search for nameincolA, replace with nameincolB
'// All occurrances will be replaced.
ChatText = Replace(ChatText, r.Value, r.Offset(, 1).Value)
End If
Next
'// Write the edited string out to a new file.
'// Also change the name of the edited file. this adds "_redact" before the extension
'// making "chat_redact.txt"
'// Note: If a file with that name already exists, IT WILL BE OVERWRITTEN.
Open Replace(ChatFile, ".txt", "_redact.txt") For Output As #iFile
Write #iFile, ChatText
'// And close.
Close #iFile
MsgBox "Finished...", vbInformation
End Sub
It assumes the active sheet contains the list of names to replace.
(Essentially the same as the previous post except the file is read in one pass rather than appending line by line and it refers to the worksheet directly rather than reading it into an array first).
Bookmarks