+ Reply to Thread
Results 1 to 7 of 7

How to find and replace a word in a sentence with VBA

  1. #1
    Registered User
    Join Date
    02-10-2010
    Location
    Sacramento, Ca, USA
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    33

    Question How to find and replace a word in a sentence with VBA

    Hi you all
    I have a table where I have two columns and in one of them there are names and in the other there should be numbers. The problem is that in the name column there are sometimes just the last name and sometimes the full name including title. I want to enter a specified number in column 2 if the name in column 1 contains "Moore". That would include Mr Simon Moore or Moore or MR Moore or Simon Moore.
    I know it is possible, but I think I was sick the day we learned how to do it. Can you please help.
    Gunilla
    Last edited by Gunilla; 04-20-2010 at 09:52 PM.

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,482

    Re: How to find and replace a word in a sentence with VBA

    To best describe or illustrate your problem you would be better off attaching a dummy workbook, the workbook should contain the same structure and some dummy data of the same type as the type you have in your real workbook - so, if a cell contains numbers & letters in this format abc-123 then that should be reflected in the dummy workbook.

    If needed supply a before and after sheet in the workbook so the person helping you can see what you are trying to achieve.

    Doing this will ensure you get the result you need!

  3. #3
    Forum Contributor
    Join Date
    04-21-2007
    Location
    Lima, Peru
    MS-Off Ver
    2000, 2007, 2010
    Posts
    674

    Re: How to find and replace a word in a sentence with VBA

    HI

    The following code applies to columns 1 and 2, but you can replace the column reference with a range if that is more appropriate.

    Please Login or Register  to view this content.
    Regards

    Jeff

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: How to find and replace a word in a sentence with VBA

    Jeff,

    You're not changing the value of c, so if What is found once, it will be found forever. However, if you were -- for example deleting the contents of c -- then this line will eventually generate an error:
    Please Login or Register  to view this content.
    ... because once c is Nothing, c.Address generates an Object variable or With block variable not set error, because VBA evaluates all arguments of a logical expression.
    Last edited by shg; 04-20-2010 at 08:18 PM.
    Entia non sunt multiplicanda sine necessitate

  5. #5
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: How to find and replace a word in a sentence with VBA

    Try this demo workbook

    It finds "Moore" in column A and adds 123 to Column B when found
    Attached Files Attached Files
    Last edited by Marcol; 04-23-2010 at 05:26 AM.

  6. #6
    Registered User
    Join Date
    02-10-2010
    Location
    Sacramento, Ca, USA
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    33

    Re: How to find and replace a word in a sentence with VBA

    Thank you, but I still don't get it. I think there are some Dim's missing. Like dim c as ? and firstAddress.
    I am enclosing an excel file to show you what I mean.
    find_replace.xls
    Gunilla

  7. #7
    Registered User
    Join Date
    02-10-2010
    Location
    Sacramento, Ca, USA
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    33

    Re: How to find and replace a word in a sentence with VBA

    Thank you I think I got it.
    Good work
    Gunilla

+ 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