Replace All Occurences Of Different Text

  • Hi All,


    May I ask how to write a search and replace macro, but instead of writing:

    Code
    Cells.Replace "OldText", "NewText"

    ... may I ask how to independently read this "search" text into a string (hopefully a string that can handle 'more' than 255 characters per cell, even in Excel 2000) then replacing that search text("OldText") with ("NewText") step-by-step?


    (Also pls. note that "OldText" can appear multiple times in one cell)


    Thanks so much in advance!!
    CG

  • Re: Search And Replace Via Reading Into A String, Then Replacing Text


  • Re: Replace All Occurences Of Different Text


    Code
    Range("A1").Value = Replace(Range("A1").Value, "Old", "New")

    [SIZE=1]Entia non sunt multiplicanda sine necessitate.[/SIZE]

  • Re: Replace All Occurences Of Different Text


    or a spin off of Dave's code



    or


    [hr]*[/hr] Auto Merged Post;[dl]*[/dl]and yet another


    Code
    Sub ReplaceAll4()
        Dim lLoop As Long
        Dim strReplace As Range
        
        'Two columns, Replace string and With String
        For Each strReplace In Sheet2.Range("A1", Sheet2.Range("A65536").End(xlUp))
            Sheet1.Cells.Replace strReplace, strReplace.Offset(0, 1)
        Next strReplace
    End Sub

    [FONT="Arial Black"][COLOR="blue"][SIZE="4"]Bill[/SIZE][/COLOR][/FONT]
    Tip: To avoid chasing code always use Option Explicit.

  • Re: Replace All Occurences Of Different Text


    Thanks Dave

    [FONT="Arial Black"][COLOR="blue"][SIZE="4"]Bill[/SIZE][/COLOR][/FONT]
    Tip: To avoid chasing code always use Option Explicit.

  • Re: Replace All Occurences Of Different Text


    I think the issue is that rng.replace doesn't work when the text in the cell is long. I didn't do anything resembling an exhaustive test, but did confirm it's so. Ergo my post, which (in my limited test) avoids the problem.

    [SIZE=1]Entia non sunt multiplicanda sine necessitate.[/SIZE]

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!