+ Reply to Thread
Page 2 of 2 FirstFirst 12
Results 16 to 24 of 24

Thread: Find and replace

  1. #16
    Registered User
    Join Date
    05-30-2009
    Location
    PA
    MS-Off Ver
    Excel 2003
    Posts
    22

    Re: Find and replace

    Hello SHG,

    I posted the workbook above in my second post.

  2. #17
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,770

    Re: Find and replace

    I already tested the code I posted against the workbook you originally posted, and it worked fine.

    Post one that doesn't work.
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  3. #18
    Forum Moderator Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2007
    Posts
    5,953

    Re: Find and replace

    Shg, I think he wants to figure out why his new code doesn't work. He tried to amend it to look at a different description or column.

  4. #19
    Registered User
    Join Date
    05-30-2009
    Location
    PA
    MS-Off Ver
    Excel 2003
    Posts
    22

    Re: Find and replace

    SHG,

    Yes, the code works for all the different column changes but not for the one for [[Description]] to be replace with column D (description)

    This is the code I used.

    Sub x()
        Dim s       As String
        Dim cell    As Range
    
        s = "[[" & Range("D1").Text & "]]"
    
        For Each cell In Range("C2", Cells(Rows.Count, "C").End(xlUp))
            cell.Replace What:=s, Replacement:=Cells(cell.Row, "D").Text, _
                         LookAt:=xlPart, MatchCase:=True
        Next cell
    End Sub
    When I run it, I get run time error 13

  5. #20
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,770

    Re: Find and replace

    Thank you, Paul.

    Try this:
    Sub x()
        Dim sRepl   As String
        Dim sOld    As String
        Dim cell    As Range
        Dim sNew    As String
    
        sRepl = "[[" & Range("D1").Text & "]]"
    
        For Each cell In Range("C2", Cells(Rows.Count, "C").End(xlUp))
            sOld = cell.Value
            sNew = Replace(sOld, sRepl, Cells(cell.Row, "D").Text)
            cell.Value = sNew
        Next cell
    End Sub
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  6. #21
    Registered User
    Join Date
    05-30-2009
    Location
    PA
    MS-Off Ver
    Excel 2003
    Posts
    22

    Re: Find and replace

    SHG,

    You are a god! That worked perfectly! BUT, there is one issue though. On the second row, it only replaced part of the data that was in the column D.

    It looks like that maybe it is limited in how many characters it does. Could that be the case?

  7. #22
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,770

    Re: Find and replace

    Sorry -- try this:
    Sub x()
        Dim sFind   As String
        Dim cell    As Range
    
        sFind = "[[" & Range("D1").Text & "]]"
    
        For Each cell In Range("C2", Cells(Rows.Count, "C").End(xlUp))
            cell.Value = Replace(cell.Value, sFind, Cells(cell.Row, "D").Value)
        Next cell
    End Sub
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  8. #23
    Registered User
    Join Date
    05-30-2009
    Location
    PA
    MS-Off Ver
    Excel 2003
    Posts
    22

    Re: Find and replace

    YOU ARE AWESOME! Works perfectly!

    I really do appreciate your time and efforts in helping me.

    I did learn a great deal on this as well.

  9. #24
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,770

    Re: Find and replace

    You're welcome. Would you please mark the thread as Solved?
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.2.0