Consulting

Results 1 to 10 of 10

Thread: String || How to do selection.replace with space?

  1. #1

    String || How to do selection.replace with space?

    Hello all.

    I would like to seek your expertise on this. My code is supposed to replace the word "Template" in my formulas and replace it with "Staff 1". I can't get it to work.
    Below is my code. Any help will be greatly appreciated.

    Selection.Replace What:="Template", Replacement:="Staff 1", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    If 'Template' is text in a cell, and I select the range of cells, it works for me

    Capture.JPG


    Do you mean that "Template" is part of a formula?
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Does this work?
    Selection.Formula = Replace(Selection.Formula,"Template", "Staff 1")
    If it is needed for a larger range, let us know.
    Last edited by jolivanes; 07-19-2018 at 08:30 PM. Reason: spelling

  4. #4
    Yes, it is part of the larger range. Basically, the code will highlight the current region, replace the word "Template" inside the formula of all the highlighted cells to "Staff 1".

    Selection.CurrentRegion.Select
    Selection.Replace What:="Template", Replacement:="Staff 1", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
    Range("A2").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Sheets("ForUpload").Select
    Range("A1").Select
    ActiveCell.Offset(1, 0).Select
    ActiveSheet.Paste

  5. #5
    Yes. The template is part of the formula.

  6. #6
    Quote Originally Posted by jolivanes View Post
    Does this work?
    Selection.Formula = Replace(Selection.Formula,"Template", "Staff 1")
    If it is needed for a larger range, let us know.
    Yes, it is part of the larger range. Basically, the code will highlight the current region, replace the word "Template" inside the formula of all the highlighted cells to "Staff 1". Below is the complete code.

    Selection.CurrentRegion.Select
    Selection.Replace What:="Template", Replacement:="Staff 1", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False
    Range("A2").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Sheets("ForUpload").Select
    Range("A1").Select
    ActiveCell.Offset(1, 0).Select
    ActiveSheet.Paste




  7. #7
    Have not tried it. See if it works.
    Change Range reference as required.
    Sub from_jindon()
        [A2:A500].Replace "Template", "Staff 1", xlPart
    End Sub

  8. #8
    Quote Originally Posted by jolivanes View Post
    Have not tried it. See if it works.
    Change Range reference as required.
    Sub from_jindon()
        [A2:A500].Replace "Template", "Staff 1", xlPart
    End Sub
    I tried it. It doesn't work as it is asking for the mapping.
    My formula looks like this;
    =Template!$A$3
    Basically, i have a worksheet named Staff 1. In my macro sheet, I need to replace the template with Staff 1 so it will populate using the data from Staff 1 worksheet.

  9. #9
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    You didn't say that it was a WS name … with a space in it

    If I put single quotes around the 'Staff 1' it works




    Sub test()
        Selection.Replace What:="Template", Replacement:="'Staff 1'", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  10. #10
    That's great. I was able to proceed with the code. Now just to clean up the extra quotes in my final report.

    Thank you for your help guys. I really appreciate it.

    Quote Originally Posted by Paul_Hossler View Post
    You didn't say that it was a WS name … with a space in it

    If I put single quotes around the 'Staff 1' it works




    Sub test()
        Selection.Replace What:="Template", Replacement:="'Staff 1'", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •