+ Reply to Thread
Results 1 to 12 of 12

Find part of text within cell and replace from another cell

  1. #1
    Registered User
    Join Date
    07-16-2008
    Location
    Syd
    Posts
    6

    Find part of text within cell and replace from another cell

    Hi,

    I need to find the "x" in range C2:C44 and replace it with a value (variable) specified in cell I2

    The catch is that I then need to be ablt to change that character when I2 changes.

    Coloum C
    *VER
    *T’x’STATUS
    *ON
    *OFF
    *T’x’BANDS

    EG

    I2 - Variable = B
    *T’x’STATUS will become *T’B’STATUS

    I2 - Variable = A
    *T’x’STATUS will become *T’A’STATUS

    But " *OFF " should not change.

    I can change *T’x’STATUS to *T’B’STATUS but then I can change *T’B’STATUS to *T’A’STATUS


    Please see attached example file as it shows it better than I can explain it.



    Thanks

    Daniel.
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor rwgrietveld's Avatar
    Join Date
    09-02-2008
    Location
    Netherlands
    MS-Off Ver
    XL 2007 / XL 2010
    Posts
    1,671
    With a simple search and replace I can to this cell formula:
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Looking for great solutions but hate waiting?
    Seach this Forum through Google

    www.Google.com
    (e.g. +multiple +IF site:excelforum.com/excel-general/ )

    www.Google.com
    (e.g. +fill +combobox site:excelforum.com/excel-programming/ )

    Ave,
    Ricardo

  3. #3
    Registered User
    Join Date
    07-16-2008
    Location
    Syd
    Posts
    6
    Hi,

    Thanks for the reply.

    That works a treat. Thanks. I was looking at it in a far more complex manor.

    However this is a test case scenario that I would like to apply in other applications.

    It would be a great help if I didn't have to edit each cell to make them compatible.

    Thanks again.

    Daniel

  4. #4
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862
    Try this in the Worksheet_Change event:
    Please Login or Register  to view this content.
    I've put this in the attached workbook as well; hope that helps.
    Attached Files Attached Files
    MatrixMan.
    --------------------------------------
    If this - or any - reply helps you, remember to say thanks by clicking on *Add Reputation.
    If your issue is now resolved, remember to mark as solved - click Thread Tools at top right of thread.

  5. #5
    Valued Forum Contributor rwgrietveld's Avatar
    Join Date
    09-02-2008
    Location
    Netherlands
    MS-Off Ver
    XL 2007 / XL 2010
    Posts
    1,671

    replace

    I've used this
    Please Login or Register  to view this content.

  6. #6
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862
    ha - yep; I started down the path i posted and thought "i should have used .Find" but then was lazy & didn't change it

    So - sydinstaller ... choice is yours; if you have a LOTS of data, then rwgrietveld's is more efficient and might save a second or so. At least you have 2 ways of doing the same thing

  7. #7
    Valued Forum Contributor rwgrietveld's Avatar
    Join Date
    09-02-2008
    Location
    Netherlands
    MS-Off Ver
    XL 2007 / XL 2010
    Posts
    1,671

    re: Find part of text within cell and replace from another cell

    Well Matrixman,

    Your code is correct but the filosophy is different. My code is run ONCE and makes the static cell content of Column C into a dynamic Formula.

    Your code changes the content of Column C every time I2 is changed.

    Both work, but the filosophy is different.

  8. #8
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862
    well .. yeah ... i figured you'd just built yours in a module so it can be invoked however the user wanted (button, sheet change, cell edit) ... I just picked on cell edit ... i suppose it depends, but why have the cell as a variable to be changed if it's only going to be done once? in any case - you win the "elegance" vote

  9. #9
    Registered User
    Join Date
    07-16-2008
    Location
    Syd
    Posts
    6
    Hi,

    Thank you to both of you.

    Both worked like a charm.

    rwgrietveld. The only bit I didn't like was how your script changed column C into a formula. I like this idea and can probably use it else where. Thank you.

    MatrixMan. This code hit the nail on the head. This is exactly what I was looking for. Thank you so much.

    I have a few questions if you don't mind me asking.

    How did you work out what character number " ' " is? This actually pointed out a typo. I have changed it to 39.

    How do I make this a macro that I can choose to run instead of being live? I.E. CTRL + O

    And lastly.

    I have virtually no idea about VB. Could you please place comments in the code to say what each part is doing. So is self explanatory but not quite all of it.

    Thanks agin.

    Daniel.

  10. #10
    Valued Forum Contributor rwgrietveld's Avatar
    Join Date
    09-02-2008
    Location
    Netherlands
    MS-Off Ver
    XL 2007 / XL 2010
    Posts
    1,671
    I often use this site http://htmlhelp.com/reference/charset/ and you can use the following code to go back and forth.

    Asc("'") and chr(39) for Apostrophe

  11. #11
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862

    further explanations ...

    Hi Daniel ... glad it worked for you ... answers to your questions are below:
    How did you work out what character number " ' " is? This actually pointed out a typo. I have changed it to 39.
    When you're in the code window, CTRL+G will bring up the "immediate" window; you can execute commands here or enquire on values during the run. To work out what character is being used you would enqure on the value of the character using a "?" such as:
    Please Login or Register  to view this content.
    which will return 146; your correct apostrophe will return 39 as you've discovered. Alternatively, to go the other way, enter:
    Please Login or Register  to view this content.
    to return the ` character. To actually execute a command, just don't use the "?" ... such as if your code stops before you exit gracefully and you've got events or screenupdating turned off, just as you would in the normal module window:
    Please Login or Register  to view this content.
    How do I make this a macro that I can choose to run instead of being live? I.E. CTRL + O
    I've used what's called an "event module" - which is a reserved module that is triggered when a user does something in the sheet - in this case, it's done when the user edits a cell in the sheet; the code checks to see if it's the cell we're interested in and if so then it runs; if not, it's skipped. Event modules are selectable from the drop-down lists when you're in an object's code window (e.g. Worksheet or Workbook). To run it on demand, you'd just move the code to a non-event module (just one that you've entered directly, either in an object or in a separate module) and allocate a button or some other control to run it (such as a shortcut as you've mentioned). I've modified the attached so that it will now run from the button on the page (not recommended as your layout doesn't allow for reasonable placement), or from CTRL+R (for "replace" - you can't use CTRL+O because that's reserved for "open"), or (still) whenever a user changes that cell. I've created a separate module (outside the sheet object, but it can be inside if you prefer - the difference is that if it's not in a sheet, you have to specify which sheet you are running the code on if it's other than the activesheet). To remove the option to run when the cell is changed, just delete the module Worksheet_Change in its entirety. Same for the button option - it's just there for demonstration ... just remove the button & code. I suspect the one you'll keep is the CTRL+R option ... to set this, just go to Tools / Macros then select Options, and you'll see where I've assigned it.

    And lastly, I have virtually no idea about VB. Could you please place comments in the code to say what each part is doing. So is self explanatory but not quite all of it.
    Sure - (it's VBA, not VB ) see the attached workbook and below.
    Please Login or Register  to view this content.
    Hope that helps. MM.
    Attached Files Attached Files

  12. #12
    Registered User
    Join Date
    07-16-2008
    Location
    Syd
    Posts
    6
    Wow.

    Thanks heaps.

    Looks like I have a bit of reading to do.

    I will have a play with it over the next few days.

    Thank you heaps.

    Much appreciated.

    Daniel.

+ 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