+ Reply to Thread
Results 1 to 10 of 10

Search and replace in the first part ONLY of cell

  1. #1
    Registered User
    Join Date
    04-22-2014
    Location
    Sweden
    MS-Off Ver
    Excel 2003
    Posts
    13

    Search and replace in the first part ONLY of cell

    Hi!

    Edit: Also I cross-posted this to the mrexcelforum, which I now realize is bad etiquette. Help still appreciated though.


    I have a work problem where my spreadsheets have the following structure due to faulty optical character recognition:


    Ringholm 8 G R hem.äg Bävik Postl 115 Ö Ämtervik
    Rinman K H B dir Johannebergsg 34 Göteborg S
    Risberg L kass Mitandersfors Bogen


    In some cases the letters in the beginning of the cells have been misread as a number (above S as 8), so I want to search and replace those numbers with the correct letter without changing the correct numbers at the end of the cells to letters too. Is there any way to write a search-and-replace code in VBA that will only implement the search-and-replace in (for example) the first five letter-spaces of the cell?

    Help would be much appreciated.
    Last edited by researchass; 07-02-2014 at 05:19 PM.

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: Search and replace in the first part ONLY of cell

    Hi,

    here is an example.. If you have "Bob8Sam8" in A1
    then
    =SUBSTITUTE(LEFT(A1,5),"8","S")&MID(A1,6,100)

    Will only look at the first 5 characters of A1 and replace any/all "8" with the letter S.

    It will give you an answer of "BobSSam8"


    I hope this helps.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Search and replace in the first part ONLY of cell

    Something like:

    Please Login or Register  to view this content.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  4. #4
    Registered User
    Join Date
    04-22-2014
    Location
    Sweden
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Search and replace in the first part ONLY of cell

    Quote Originally Posted by MarvinP View Post
    Hi,

    here is an example.. If you have "Bob8Sam8" in A1
    then
    =SUBSTITUTE(LEFT(A1,5),"8","S")&MID(A1,6,100)

    Will only look at the first 5 characters of A1 and replace any/all "8" with the letter S.

    It will give you an answer of "BobSSam8"


    I hope this helps.

    Hi,

    Thank you for your help. However when I try to paste your formula I get the error in formula-message... Am I missing something?

  5. #5
    Registered User
    Join Date
    04-22-2014
    Location
    Sweden
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Search and replace in the first part ONLY of cell

    edit.... duplicate

  6. #6
    Registered User
    Join Date
    04-22-2014
    Location
    Sweden
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Search and replace in the first part ONLY of cell

    Quote Originally Posted by xladept View Post
    Something like:

    Please Login or Register  to view this content.
    Hi, thank you for your response. When I try to run the code on my column A i get the error message "compilation error: matrix expected" (in swedish).

    Also looking at the code - though I'm no programmer - doesn't it say "if cell is longer then six, then exit sub"? If that's the case that's quite not what I want to do. I still want to search and replace, but just in the first six spaces.

  7. #7
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Search and replace in the first part ONLY of cell

    To run it you need to have a cell selected. The < and > symbols relate to the positions on a number line, going to the left means less - to the right more

  8. #8
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: Search and replace in the first part ONLY of cell

    Hi,
    Find my sample workbook and see if you pasted the formula into the wrong cell??

    It is a good chance that I did a function (Substitute()) that isn't in 2003 excel and started working with the 2007 version. That would explain why you get an error.

    Sorry. Even Excel changes with more new functions in newer versions. If you had 2007 Excel or better, I bet my formula would work.
    Attached Files Attached Files
    Last edited by MarvinP; 07-02-2014 at 06:45 PM.

  9. #9
    Registered User
    Join Date
    04-22-2014
    Location
    Sweden
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Search and replace in the first part ONLY of cell

    Quote Originally Posted by MarvinP View Post
    Hi,
    Find my sample workbook and see if you pasted the formula into the wrong cell??

    It is a good chance that I did a function (Substitute()) that isn't in 2003 excel and started working with the 2007 version. That would explain why you get an error.

    Sorry. Even Excel changes with more new functions in newer versions. If you had 2007 Excel or better, I bet my formula would work.
    Thank you! I'm not sure what the problem was (maybe that the commands in my excel are in Swedish?), but now it worked like a charm. Recorded a macro using nested variations of this code and posted it pasted it to my main macro.

  10. #10
    Registered User
    Join Date
    04-22-2014
    Location
    Sweden
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Search and replace in the first part ONLY of cell

    Quote Originally Posted by xladept View Post
    To run it you need to have a cell selected. The < and > symbols relate to the positions on a number line, going to the left means less - to the right more
    I rewrote the code like this (selecting column A):

    Please Login or Register  to view this content.
    But I still get this error message:

    matrixexpected.jpg

    Just realized it might be because I selected a column instead of a cell?

    Anyway, the other solution worked, but so its not that important. Thanks anyhow!

  11. #11
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Search and replace in the first part ONLY of cell

    You're welcome and thanks for the rep!
    Last edited by xladept; 07-03-2014 at 03:20 PM.

+ 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. Search and Replace part of a formula
    By onemoremile in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-22-2013, 02:16 PM
  2. Modify Macro Search & Replace Formatting for part of cell
    By tkeiffer in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-02-2011, 05:48 PM
  3. Search and Replace Part of Formulas
    By alainsi in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-30-2009, 05:48 AM
  4. Replies: 4
    Last Post: 06-15-2007, 03:50 AM
  5. Search &amp;amp; Replace Formatting for part of cell
    By Tanya B in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-30-2005, 09:05 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