+ Reply to Thread
Results 1 to 24 of 24

VBA: Tweak code to use .replace with no character limitation?

  1. #1
    Forum Contributor
    Join Date
    08-25-2015
    Location
    London
    MS-Off Ver
    MS 365
    Posts
    213

    Question VBA: Tweak code to use .replace with no character limitation?

    Hi all,

    I have this code (credits to @Fluff13) that replaces values on one worksheet with values on another (using a lookup value that is consistent across the two worksheets).
    Basically, I am using this to translate content across multiple languages, where the lookup value = the language. See here for the initial thread.

    Please Login or Register  to view this content.
    If I use this with single values, e.g. replace "apple" with "Apfel", it works fine, however, as soon as I add whole sentences to replace, I get a "type mismatch" error on line

    .Replace Ws.Cells(r, 2), Ws.Cells(r, c), xlPart, , False, , False, False

    My assumption is that .replace cannot handle the replacement if the cell exceeds a certain character count.

    My question is: is there a way to tweak this to allow me to replace full sentences, ideally lifting re character restriction completely?

    Hope this works somehow?
    Thanks all!

  2. #2
    Banned User!
    Join Date
    01-17-2021
    Location
    Omaha, NE
    MS-Off Ver
    office 2016
    Posts
    211

    Re: VBA: Tweak code to use .replace with no character limitation?

    well first of all is your code right?
    Please Login or Register  to view this content.
    this says

    https://docs.microsoft.com/en-us/off...place-function

    Replace(expression, find, replace, [ start, [ count, [ compare ]]])

    you need 3 arguments not optional at beginning. so your 'xlPart' argument is technically in the 'replace' part of what microsoft says. that is supposed to be replacement string not enumeration.

  3. #3
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: VBA: Tweak code to use .replace with no character limitation?

    kingofcamden,

    Range.Replace method has 255 character limitation, so perhaps...
    Please Login or Register  to view this content.

  4. #4
    Banned User!
    Join Date
    01-17-2021
    Location
    Omaha, NE
    MS-Off Ver
    office 2016
    Posts
    211

    Re: VBA: Tweak code to use .replace with no character limitation?

    thank you jindon. on microsoft page, it does not say limitation. dont know why?

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: VBA: Tweak code to use .replace with no character limitation?

    omahaNative_1023,

    You are talking about completely different "REPLACE".
    You seem like someone with other username before.

  6. #6
    Banned User!
    Join Date
    01-17-2021
    Location
    Omaha, NE
    MS-Off Ver
    office 2016
    Posts
    211

    Re: VBA: Tweak code to use .replace with no character limitation?

    what? what other replace? there is only one function. right? is 2 different ones? what username. you asking of my name?

  7. #7
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: VBA: Tweak code to use .replace with no character limitation?


    At least 3 Replace under VBA, a function, a worksheet function and a method …

  8. #8
    Forum Contributor
    Join Date
    08-25-2015
    Location
    London
    MS-Off Ver
    MS 365
    Posts
    213

    Re: VBA: Tweak code to use .replace with no character limitation?

    Hi all!

    Thanks so much for all the input and help - appreciate it!

    @Jindon: Your code works perfectly!! Made my day!! Thanks so much!!

    The only question is, how would I need to tweak this code if:
    1. the lookup value (language) is on (destination) sheet "Translator" in range D9:D100 and on (source) sheet "Translations" in range C2:CP2
    2. the cells to translate are on sheet "Translator" in range E9:E100 and the sentences with the master copy in range B3:B102 (translations in range C3:CP102)?

    Couldn't really work it out, sorry. Added some comments to the below, but unsure how I can change the cells/ranges to look at & replace?

    Please Login or Register  to view this content.
    Last edited by kingofcamden; 03-14-2021 at 10:29 AM.

  9. #9
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: VBA: Tweak code to use .replace with no character limitation?

    It is too hard without seeing actual data layouts.

    If you upload a workbook with the correct layout, it will help.

  10. #10
    Forum Contributor
    Join Date
    08-25-2015
    Location
    London
    MS-Off Ver
    MS 365
    Posts
    213

    Re: VBA: Tweak code to use .replace with no character limitation?

    Hey jindon,

    Sure, sorry for not providing an example earlier.

    I've re-attached the old file with the updated ranges. It's all dummy copy, but hopefully it helps?

    Thanks so much!

  11. #11
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: VBA: Tweak code to use .replace with no character limitation?

    Do you really have all those complete blank row(s) & column(s) around data range?

    You also have "Example:" in C9.
    Is this correct?

  12. #12
    Forum Contributor
    Join Date
    08-25-2015
    Location
    London
    MS-Off Ver
    MS 365
    Posts
    213

    Re: VBA: Tweak code to use .replace with no character limitation?

    I do There's some other content there that I cannot move.

    Oh, yes! Sorry. please ignore C9 - that was really just to say this is all example/dummy content.

    Thank you!

  13. #13
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: VBA: Tweak code to use .replace with no character limitation?

    Then try change to
    Please Login or Register  to view this content.

  14. #14
    Forum Contributor
    Join Date
    08-25-2015
    Location
    London
    MS-Off Ver
    MS 365
    Posts
    213

    Re: VBA: Tweak code to use .replace with no character limitation?

    Thank you! Tried this, but doesn't seem to work...there's no error message, but values don't get replaced.

    Think I understand now that

    [b2].CurrentRegion.Value is considered the 'start' point on the "Translations" worksheet and
    [d9].CurrentRegion the 'start' point on the "Translator" worksheet.

    However, if I compare to the old (working) code, I can see that

    [d9].CurrentRegion actually 'started' one row up from the value (i.e. it was [b1].CurrentRegion in the old code, but the content didn't 'start' before cell b2).
    So I tried changing [d9].CurrentRegion to [d8].CurrentRegion, but that also didn't work.

    Saw you changed some of the values in this part:

    Please Login or Register  to view this content.
    Maybe it's connected to that? Tried to play around with this, but no luck...got a 'type mismatch' error most of the time.

    Sorry for the hassle...appreciate your help!

  15. #15
    Forum Contributor
    Join Date
    08-25-2015
    Location
    London
    MS-Off Ver
    MS 365
    Posts
    213

    Re: VBA: Tweak code to use .replace with no character limitation?

    Hey jindon,

    Ahh, I think I did it! Basically just updated the following in the old code

    [b1].CurrentRegion to be [d9].CurrentRegion

    That seems to have done the trick

    Please Login or Register  to view this content.
    Yaaay! So happy with this! Thanks so much again for your help!
    All the best!!

  16. #16
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: VBA: Tweak code to use .replace with no character limitation?

    Oh, yes! Sorry. please ignore C9 - that was really just to say this is all example/dummy content.
    Did you clear C9 before you run the code?

  17. #17
    Forum Contributor
    Join Date
    08-25-2015
    Location
    London
    MS-Off Ver
    MS 365
    Posts
    213

    Re: VBA: Tweak code to use .replace with no character limitation?

    Hey jindon,

    I didn't, but I just noticed that weirdly, the code needs a blank 9 row on "Translator" worksheet and a blank 2 row on "Translations" worksheet.
    It doesn't seem to work if there is anything in those rows...

    I now tried to copy everything over from the example spreadsheet to my master, but unfortunately, it doesn't work anymore...I can't tell why.
    It's the exact same setup...all in the same cells, but I do have other content on those sheets...tried everything really. Also checked references, but they are the same across both sheets.
    I haven't altered the code so it's so weird that it works on one worksheet, but not on the other... do you have any idea why? Are there any other cells that would need to be left blank?
    Did a few spot checks, but no luck...

    Thank you!

  18. #18
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: VBA: Tweak code to use .replace with no character limitation?

    That's why I asked the question.

    Unlike formula, getting correct data range is the one of the most important factor in the vba.

  19. #19
    Forum Contributor
    Join Date
    08-25-2015
    Location
    London
    MS-Off Ver
    MS 365
    Posts
    213

    Re: VBA: Tweak code to use .replace with no character limitation?

    Ah, okay! Makes sense! Got it to work now

    Basically, row 9 on "Translator" worksheet & row 2 and column A on "Translations" worksheet need to be blank for it to work

    So happy it works now. Thanks for all the tips!!

  20. #20
    Forum Contributor
    Join Date
    08-25-2015
    Location
    London
    MS-Off Ver
    MS 365
    Posts
    213

    Question Re: VBA: Tweak code to use .replace with no character limitation?

    Hey jindon,

    Hope all is well! Sorry for another question on this...I don't know what happened, but suddenly the code stopped working.

    First I assigned the macro to a button, but received an error message on click that macros would need to be enabled (which they were, i.e. I could run the code without clicking the button).
    Then I tried an activex button and moved the code from a module directly to the worksheet (didn't know how to link an activex button to a module).
    Now I can execute the code again, however, it doesn't work anymore. I tried to go back to the original state, but that also doesn't work.

    Are there maybe other cells that also need to be cleared for it to work?
    Did anything change in Excel (since I am now using activex button) so maybe the code can't run anymore? Checked the references, but nothing has changed.

    I have quite a bit of other content on my master sheet, however, made sure that I keep the range we spoke about clear of content:
    row 9 on "Translator" worksheet & row 2 and column A on "Translations" worksheet.

    All used to work fine for a few days :-(

    Example attached again... does this run for you?

    Many thanks - appreciate your help!
    Last edited by kingofcamden; 03-18-2021 at 05:10 AM.

  21. #21
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: VBA: Tweak code to use .replace with no character limitation?

    Change my code posted in #13
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.

  22. #22
    Forum Contributor
    Join Date
    08-25-2015
    Location
    London
    MS-Off Ver
    MS 365
    Posts
    213

    Re: VBA: Tweak code to use .replace with no character limitation?

    Hey jindon,

    Thanks for looking into this. Sadly, I already made that change, but noticed that some other bits were wrong now, i.e.

    Please Login or Register  to view this content.
    had changed (no idea how I did this, sorry!).

    Looks like it's working again in the example spreadsheet, but still not in my master...can't really explain it. The ranges are exactly the same + I made sure to clear the content areas that need to be empty via the additional code I added. This is the code I am using now:

    Please Login or Register  to view this content.
    Can you see anything that would stop this from working?
    Thanks so much!
    Last edited by kingofcamden; 03-18-2021 at 05:28 AM.

  23. #23
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,523

    Re: VBA: Tweak code to use .replace with no character limitation?

    The changed code is already tested here with your latest workbook.

    I'm getting tired, so you need to upload the actual 2 worksheets you are working with.

  24. #24
    Forum Contributor
    Join Date
    08-25-2015
    Location
    London
    MS-Off Ver
    MS 365
    Posts
    213

    Re: VBA: Tweak code to use .replace with no character limitation?

    Hey jindon,

    So sorry about this! I played around with it all morning and think I know what it was...seems like more ranges need to be blank for it to work, i.e. no content is allowed to the left of the range that holds the lookup value on the 'Translator' tab.

    Got it to work again! Thanks for all your help - appreciate it!

+ 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. replace EACH Nth occurrence a character in a string with another character
    By mattman7 in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 06-23-2016, 12:23 PM
  2. Data Validation character limitation issue
    By sj80 in forum Excel General
    Replies: 21
    Last Post: 05-30-2014, 10:22 AM
  3. [SOLVED] FormulaArray 255 charter limitation .Replace workaround no-longer works/unstable
    By stewegg in forum Excel Programming / VBA / Macros
    Replies: 31
    Last Post: 01-07-2014, 07:55 PM
  4. Cell character limitation and recognition
    By erisaaka in forum Excel General
    Replies: 7
    Last Post: 03-20-2013, 04:03 PM
  5. Best way to 'tweak' my code?
    By [email protected] in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-29-2008, 10:10 AM
  6. Character Limitation
    By hicksviv in forum Excel General
    Replies: 5
    Last Post: 04-13-2006, 11:36 AM
  7. Dreaded 911 cell character limitation
    By mdengler in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-01-2006, 10:32 AM

Tags for this Thread

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