+ Reply to Thread
Results 1 to 10 of 10

Duplicate Line and Replace word

  1. #1
    Registered User
    Join Date
    07-25-2019
    Location
    CA
    MS-Off Ver
    365
    Posts
    10

    Duplicate Line and Replace word

    Hello, I have a file that has a sentence on one row and a second tab with find/replace words. I am hoping through VBA or something similar I could run a tool that would review my file (approx 50,000 lines) and if the word in Column 1 is present, it would duplicate the row x times, depending on how many replace words there are and change the word in the sentence. Attached is a very simple sample file of what I am trying to accomplish.

    Example:
    Sheet 1: The Cat jumped over the Dog

    Sheet 2:
    Find / Replace With
    Dog / Frog
    Dog / Turtle
    Dog / Pig
    Dog / Horse

    Sheet 3:
    The Cat jumped over the Dog
    The Cat jumped over the Frog
    The Cat jumped over the Turtle
    The Cat jumped over the Pig
    The Cat jumped over the Horse

    Thanks for any help you can offer!
    Attached Files Attached Files

  2. #2
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,920

    Re: Duplicate Line and Replace word

    This was a fun little project and it demonstrates the power of using Excel Tables even in VBA.

    Enter your sentence in cell B1 on the starting point sheet.

    The Replace word list contains a table. Fill in your substitutions here. Tables know how big they are. So copy and paste your data in or keep adding items to the bottom of the list.

    The results are shown on the End Result Page.

    The code is very simple and it shows how you can use a table to loop through data. I also made use of named ranges. THis means you can add rows above cell B1 and the named range will follow the cell. Named ranges help keep cells associated with code. VBA doesn't know when you move things around on a sheet, so a named range gives you the flexibility to do this without having to change the code.

    Please Login or Register  to view this content.
    Attached Files Attached Files
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

  3. #3
    Registered User
    Join Date
    07-25-2019
    Location
    CA
    MS-Off Ver
    365
    Posts
    10

    Re: Duplicate Line and Replace word

    This is awesome, I forgot one detail. On the "Starting Point" tab there are two columns. Column A has the Sentence and Column B has the persons Name. I need column B (The name) to be included on the new file/lines. Updated sample attached.

    Thanks!!
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    07-25-2019
    Location
    CA
    MS-Off Ver
    365
    Posts
    10

    Re: Duplicate Line and Replace word

    Also, this only applies to the first row, however, my source Starting Point is 50,000 rows. Can it be expanded to include all 50,000 rows on the starting point tab?

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Duplicate Line and Replace word

    Try
    Please Login or Register  to view this content.

  6. #6
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,466

    Re: Duplicate Line and Replace word

    Alternative:
    PHP Code: 
    Option Explicit
    Sub replaceSP
    ()
    Dim lr&, i&, j&, k&, starrepres(1 To 100001 To 2)
    With Sheets("Starting Point")
        
    lr = .Cells(Rows.Count"A").End(xlUp).Row
        star 
    = .Range("A1:B" lr).Value ' array of starting point
    End With
    With Sheets("Replace word List")
        lr = .Cells(Rows.Count, "A").End(xlUp).Row
        rep = .Range("A2:B" & lr).Value ' 
    array of replaced word list
    End With
    For 1 To UBound(star)
        
    1res(k1) = star(i1): res(k2) = star(i2' save 1st original row into result array
        For j = 1 To UBound(rep)
            If InStr(1, star(i, 1), rep(j, 1)) Then ' 
    if found
                k 
    1res(k1) = Replace(star(i1), rep(j1), rep(j2)) ' replace then save to result array
                res(k, 2) = star(i, 2)
            End If
        Next
    Next
    With Sheets("End Result")
        .Range("A1").Resize(k, 2).Value = res ' 
    copy result array to sheet
    End With
    End Sub 
    Attached Files Attached Files
    Quang PT

  7. #7
    Registered User
    Join Date
    07-25-2019
    Location
    CA
    MS-Off Ver
    365
    Posts
    10

    Re: Duplicate Line and Replace word

    Thanks for trying. Unfortunately, this didn't work. I am getting a Run-Time Error 1004.

    Sub test()
    Dim a, b, c, i As Long, ii As Long, n As Long
    a = Sheets("starting point").[a1].CurrentRegion.Value
    b = Sheets("Replace word List").[a1].CurrentRegion.Value
    ReDim c(1 To UBound(a, 1) * UBound(b, 1), 1 To 4)
    For i = 1 To UBound(a, 1)
    For ii = 1 To UBound(b, 1)
    n = n + 1: c(n, 4) = a(i, 2)
    c(n, 1) = Replace(a(i, 1), b(ii, 1), b(ii, 2))
    Next ii, i
    Sheets("end result").[a1].Resize(n, 4) = c <--- log shows issue is here
    End Sub

  8. #8
    Registered User
    Join Date
    07-25-2019
    Location
    CA
    MS-Off Ver
    365
    Posts
    10

    Re: Duplicate Line and Replace word

    Quote Originally Posted by bebo021999 View Post
    Alternative:
    PHP Code: 
    Option Explicit
    Sub replaceSP
    ()
    Dim lr&, i&, j&, k&, starrepres(1 To 100001 To 2)
    With Sheets("Starting Point")
        
    lr = .Cells(Rows.Count"A").End(xlUp).Row
        star 
    = .Range("A1:B" lr).Value ' array of starting point
    End With
    With Sheets("Replace word List")
        lr = .Cells(Rows.Count, "A").End(xlUp).Row
        rep = .Range("A2:B" & lr).Value ' 
    array of replaced word list
    End With
    For 1 To UBound(star)
        
    1res(k1) = star(i1): res(k2) = star(i2' save 1st original row into result array
        For j = 1 To UBound(rep)
            If InStr(1, star(i, 1), rep(j, 1)) Then ' 
    if found
                k 
    1res(k1) = Replace(star(i1), rep(j1), rep(j2)) ' replace then save to result array
                res(k, 2) = star(i, 2)
            End If
        Next
    Next
    With Sheets("End Result")
        .Range("A1").Resize(k, 2).Value = res ' 
    copy result array to sheet
    End With
    End Sub 
    Thanks for this suggestion unfortunately when I run it I am getting a Run Time Error 9 and the debug is highlighting this line:
    k = k + 1: res(k, 1) = Replace(star(i, 1), rep(j, 1), rep(j, 2)) ' replace then save to result array

  9. #9
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,466

    Re: Duplicate Line and Replace word

    Quote Originally Posted by BdoggCA View Post
    Thanks for this suggestion unfortunately when I run it I am getting a Run Time Error 9 and the debug is highlighting this line:
    k = k + 1: res(k, 1) = Replace(star(i, 1), rep(j, 1), rep(j, 2)) ' replace then save to result array
    As you see tts works for me, with that file.
    Make sure all your sheets have same names as in sample file.
    Otherwhile you have to adapt the new names.

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Duplicate Line and Replace word

    Try
    Please Login or Register  to view this content.

+ 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. [SOLVED] Macro to duplicate a line but replace data in cells from a comment column
    By Kalamari in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-26-2021, 09:30 AM
  2. Replies: 4
    Last Post: 10-21-2020, 11:42 PM
  3. [SOLVED] Excel to Word Find and Replace changes line spacing.
    By pichon in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-27-2018, 12:47 AM
  4. Excel VBA - Word to search for part of a word and replace full word
    By charlie543 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-24-2016, 02:56 AM
  5. If line is 'Checked' replace line in Original data...... PLEASE HELP :(
    By evansmike881 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-02-2016, 10:09 AM
  6. [SOLVED] Help with macro to find a specific word and replace the entire cell with the return word.
    By BG1983 in forum Excel Programming / VBA / Macros
    Replies: 21
    Last Post: 08-24-2015, 09:40 PM
  7. Replies: 1
    Last Post: 01-26-2012, 10:06 AM

Tags for this Thread

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