+ Reply to Thread
Results 1 to 14 of 14

Find and replace script / macro?

Hybrid View

  1. #1
    Registered User
    Join Date
    03-08-2004
    Posts
    50

    Find and replace script / macro?

    Help Needed: Find and replace script / macro?

    Hi,

    I'm not sure what type of function is needed for what I'm describing below. I'm thinking it is a script or macro? I don't know anything about writing scripts or macros and I am quite new to excel. I'm hoping someone can help.

    I'm trying to learn chinese and am making chinese flashcards for myself.

    The fact that some of the cells have chinese characters probably doesn't matter, but I just want to be thorough in my description.



    Example:
    A---------------B---------------C
    dog-------------gǒu-------------The dog is in the house. Na ge gǒu zai jiālǐ.
    家裡-------------jiālǐ-------------The dog is in the 家裡. Na ge gǒu zai jiālǐ.

    I need for the script to take the value of Cell in Column A "dog" and the value of Cell in Column B "gǒu" and replace those with " ~ " in Cell C.

    Thus, after running the script/macro the output will be:
    A---------------B---------------C
    dog-------------gǒu-------------The ~ is in the house. Na ge ~ zai jiālǐ.
    家裡-------------jiālǐ-------------The dog is in the ~ . Na ge gǒu zai ~ .



    I will be eternally grateful for help on this!!


    P.S. - the formatting for the tabs look strange (i used ---- instead). I'm also attaching a sample of the excel file so that it can be read more easily.
    Attached Files Attached Files
    Last edited by luu980; 06-22-2008 at 10:21 AM.

  2. #2
    Forum Expert Simon Lloyd's Avatar
    Join Date
    03-02-2004
    Location
    locked in the cage
    MS-Off Ver
    All the ones my homepage shows
    Posts
    3,161
    Perhaps this will suffice:
    Sub Find_N_Replace_All()
    Dim Rng As Range, MyCell As Range
    Set Rng = Range("A2:B" & Range("B" & Rows.Count).End(xlUp).Row)
    For Each MyCell In Rng
       Columns(3).Replace What:=MyCell, Replacement:="~", LookAt:=xlPart, SearchOrder _
            :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
    Next MyCell
    End Sub
    Not all forums are the same - seek and you shall find

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Simon's code replaces every instance of dog in column C with a tilde, rather than just the instance on the same row. If that's not what you want, then change the line to:
            Cells(MyCell.Row, "C").Replace What:=MyCell.Value, Replacement:="~", _
            LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
            SearchFormat:=False, ReplaceFormat:=False

  4. #4
    Forum Expert Simon Lloyd's Avatar
    Join Date
    03-02-2004
    Location
    locked in the cage
    MS-Off Ver
    All the ones my homepage shows
    Posts
    3,161
    Well caught shg! i assumed that the user was going to have a complete list in column A & B but good thinking!

    By the way i forgot about blanks with my "blanket" code so:
    Sub Find_N_Replace_All()
    Dim Rng As Range, MyCell As Range
    Set Rng = Range("A2:B" & Range("B" & Rows.Count).End(xlUp).Row)
    For Each MyCell In Rng
    if mycell.value="" then GoTo Nxt
       Columns(3).Replace What:=MyCell, Replacement:="~", LookAt:=xlPart, SearchOrder _
            :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
    Nxt:
    Next MyCell
    End Sub

  5. #5
    Registered User
    Join Date
    03-08-2004
    Posts
    50
    Thank you for the code. I'm going to try it out asap!

    Ok, this is a really really stupid question I know. I am a total beginner to excel. How do I implement this code? I'm using excel 2007.

    *Sigh*, very sorry about this question

  6. #6
    Forum Expert Simon Lloyd's Avatar
    Join Date
    03-02-2004
    Location
    locked in the cage
    MS-Off Ver
    All the ones my homepage shows
    Posts
    3,161
    Hold Alt and press F11, on the left hand side you see your worksheet names and ThisWorkbook right click there and choose INSERT, MODULE and paste thye code in, you can then run it from the worksheet by selecting VIEW in the menubar then on the left MACROS then choose the macro you want to run.

  7. #7
    Registered User
    Join Date
    03-08-2004
    Posts
    50

    Re: Find and replace script / macro?

    I think the problem is solved.

    The final code below appears to work correctly!:
    Sub Find_N_Replace_All()
    Dim Rng As Range, MyCell As Range
    Set Rng = Range("A2:B" & Range("B" & Rows.Count).End(xlUp).Row)
    For Each MyCell In Rng
    If MyCell.Value = "" Then GoTo Nxt
       Cells(MyCell.Row, "C").Replace What:=MyCell.Value, Replacement:="~", _
            LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
            SearchFormat:=False, ReplaceFormat:=False
    Nxt:
    Next MyCell
    End Sub
    Thank you so much for the help everyone!!
    Last edited by luu980; 03-09-2009 at 03:07 PM.

  8. #8
    Forum Contributor Tarball's Avatar
    Join Date
    04-21-2007
    Location
    Atlanta, GA
    Posts
    166

    Re: Find and replace script / macro?

    If you find that you still have problems then see this thread: http://www.excelforum.com/excel-prog...character.html

    Look towards the bottom to my post where you will find a Replace String function that you can manipulate and use. This has worked quite well for me on many projects.
    Reach me at excel_help at bellsouth dot net

+ 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.6.0 RC 1