+ Reply to Thread
Results 1 to 14 of 14

VBA Mass find and replace from 2 columns into .txt file

Hybrid View

  1. #1
    Registered User
    Join Date
    12-13-2016
    Location
    Denmark
    MS-Off Ver
    2010
    Posts
    6

    VBA Mass find and replace from 2 columns into .txt file

    I know there are many posts like this one, but i spent 2 hours last night trying to get the macros i found to work, without luck.

    Im doing a study at my university, where i have just above 14000 lines of chat text. Now i need to change everybodys name so will be anonymous. Now i have my excel sheet with 895 rows of original names in column A, and 895 rows of new names in column B, thats need to be replaced in my .txt document.


    After some time on Google i discovered VBA macros would be the best soulution for me. Im very noobish at programming, so if someone could help me with a VBA macro here, i shall name my first born after you! (Or maybe something less dramatic, i'll eat a cake in your honor).
    Last edited by mexikaan; 12-13-2016 at 06:11 AM.

  2. #2
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: VBA Mass find and replace from 2 columns into .txt file

    Post a sample of your chat text and the worksheet containing the names.

  3. #3
    Registered User
    Join Date
    12-13-2016
    Location
    Denmark
    MS-Off Ver
    2010
    Posts
    6

    Re: VBA Mass find and replace from 2 columns into .txt file

    Sure thing, i provided here a sample of the chat and the names that are replacing. What i forgot to specify in my first post was that many of the names will occur many times, and will need to be replace in each instance.


    Also, the names are enclosed in < > and those need to stay.
    Attached Files Attached Files

  4. #4
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: VBA Mass find and replace from 2 columns into .txt file

    What if a name is mentioned in the chat text...? That will not have the bounding '<' & '>' so what's to do then?

  5. #5
    Registered User
    Join Date
    12-13-2016
    Location
    Denmark
    MS-Off Ver
    2010
    Posts
    6

    Re: VBA Mass find and replace from 2 columns into .txt file

    All of those also needs to be replaced, hope that doesnt pose a problem with the < > and lack of in other instances

  6. #6
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: VBA Mass find and replace from 2 columns into .txt file

    Can just ignore the '<' & '>' but it was very useful to be able to delimit the search terms with the angle brackets.

    I'd be wary of just doing a blanket replace of a name. You might have someone using 'Star' as a name. Blindly replace 'star' with something else will also affect words like 'stare' ... so there's a compromise to be made:

    Can replace all instances with angle brackets -that'll cause no problems.
    Where a name is mentioned in text then make an assumption that it will be bounded with spaces either side and replace ' name ' with ' newname ' but that will not catch instances where the name is the last word in the message text - but that can also be coded for. And so it goes on.

    Admittedly the chances of these exceptions get less and less and I really can't think of others, but that's the choice to be made.
    Last edited by cytop; 12-13-2016 at 07:07 AM.

  7. #7
    Registered User
    Join Date
    12-13-2016
    Location
    Denmark
    MS-Off Ver
    2010
    Posts
    6

    Re: VBA Mass find and replace from 2 columns into .txt file

    Well they way i see it, i would rather have it replace too much than too little. Also we are dealing with Twitch names, so they tend to be somewhat different and weird from normal words used If it would happen like you mentioned star - stare, im ok with that, the main issue is removing participants names.

  8. #8
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,358

    Re: VBA Mass find and replace from 2 columns into .txt file

    See how far this gets you.
    Assuming textfile and XL-file are in the same dir. Otherwise you have to amend path and filename.

    Sub ReplaceStringInFile()
    
        Dim sBuf As String, sTemp As String
        Dim iFileNum As Integer, sFileName As String
    
    ' Edit as needed
        sFileName = ThisWorkbook.Path & "\" & "chat.txt"
    
        iFileNum = FreeFile
        Open sFileName For Input As iFileNum
    
        Do Until EOF(iFileNum)
            Line Input #iFileNum, sBuf
            sTemp = sTemp & sBuf & vbCrLf
        Loop
        Close iFileNum
        
        sn = Cells(1).CurrentRegion.Value
        For i = 1 To UBound(sn)
            sTemp = Replace(sTemp, sn(i, 1), sn(i, 2))
        Next
    
        iFileNum = FreeFile
        Open sFileName For Output As iFileNum
        Print #iFileNum, sTemp
        Close iFileNum
    
    End Sub
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  9. #9
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: VBA Mass find and replace from 2 columns into .txt file

    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).
    Last edited by cytop; 12-13-2016 at 08:14 AM.

  10. #10
    Registered User
    Join Date
    12-13-2016
    Location
    Denmark
    MS-Off Ver
    2010
    Posts
    6

    Re: VBA Mass find and replace from 2 columns into .txt file

    Thank you guys so much! Will test out later on today when im home, sitting on my chromebook atm so not able to use excel Will post feedback asap.

    On a sidenote, almost 900 names and 14000+ lines of chat, how long will it take to run the script, on a rather new high-end computer? Not that time is an issue, just so i know it didnt crash or anything, thats its actually still doing stuff

  11. #11
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,358

    Re: VBA Mass find and replace from 2 columns into .txt file

    @ cytop

    For speed better to read names in Array then reading line by line of worksheet.
    So combining your reading of textfile in one go and my loop to change names should give best results.
    Last edited by bakerman2; 12-13-2016 at 08:26 AM.

  12. #12
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: VBA Mass find and replace from 2 columns into .txt file

    I know - keeping it simple for the OP. One step at a time.

  13. #13
    Registered User
    Join Date
    12-13-2016
    Location
    Denmark
    MS-Off Ver
    2010
    Posts
    6

    Re: VBA Mass find and replace from 2 columns into .txt file

    Alright got home and tested it, and it worked! It worked perfect, and the macro took about 5 sec to run and it was done! :D

    Thank you sooo much cytop, and bakerman2, for your huge help with this! You saved my group a lot of work! Also wanna thank you for how you handled it, you really went above and beyond with making the script to really tailor my needs! Any way to send you any kind of e-points in here, besides marking this as solved?

    We shall be having cake tomorrow in your honor!

  14. #14
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,358

    Re: VBA Mass find and replace from 2 columns into .txt file

    Save a slice for me, yes.

    You're welcome and thanks for adding rep points.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Mass Find Replace VBA
    By robtuby in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-18-2014, 08:23 AM
  2. Mass Find and Replace?
    By Kinanik in forum Excel General
    Replies: 5
    Last Post: 06-12-2012, 01:49 PM
  3. [SOLVED] Excel 2007 : Mass Find and Replace
    By kikokazuma in forum Excel General
    Replies: 4
    Last Post: 04-30-2012, 08:43 AM
  4. Mass find and replace
    By Watoth in forum Excel General
    Replies: 1
    Last Post: 11-01-2010, 02:09 PM
  5. Mass find and replace
    By coletteno1 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 09-17-2010, 07:27 AM
  6. Mass Find and Replace Macro
    By Singularity7250 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-09-2010, 08:11 AM
  7. Mass Find & Replace Script?
    By TyneeTom in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-24-2009, 03:41 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1